asp.net repeater pagination Example (Pagedatasource) _ Practical Tips

Source: Internet
Author: User
asp.net provides three powerful list controls: The DataGrid, DataList, and Repeater controls, but only the DataGrid controls provide paging functionality. Relative datagrid,datalist and repeater controls have a higher style customization, so many times we prefer to use DataList or Repeater controls to display data.

There are several ways to implement pagination for a DataList or Repeater control:
1. Write a method or stored procedure that returns the data table (DataTable) that needs to be displayed according to the number of pages passed in
2, using the PagedDataSource class (located in the System.Web.UI.WebControls namespace)

This article mainly explains how to use the PagedDataSource class to implement the paging display of DataList and Repeater controls. The PagedDataSource class is also used inside the DataGrid control, and the PagedDataSource class encapsulates the properties of the DataGrid control, which enable the DataGrid to execute pagination.

Partial public properties of the PagedDataSource class:
AllowCustomPaging Gets or sets a value that indicates whether to enable custom paging.
AllowPaging Gets or sets a value that indicates whether paging is enabled.
Count gets the number of items to use from the data source.
CurrentPageIndex Gets or sets the index of the current page.
DataSource Gets or sets the data source.
Datasourcecount gets the number of items in the data source.
Firstindexinpage gets the first index in the page.
Iscustompagingenabled gets a value that indicates whether to enable custom paging.
Isfirstpage gets a value that indicates whether the current page is the homepage.
Islastpage gets a value that indicates whether the current page is the last page.
Ispagingenabled gets a value that indicates whether paging is enabled.
IsReadOnly gets a value that indicates whether the data source is read-only.
IsSynchronized gets a value that indicates whether access to the data source (thread-safe) is synchronized.
PageCount gets the total number of pages required to display all items in the data source.
PageSize Gets or sets the number of items to display on a single page.
Virtualcount Gets or sets the number of actual items in the data source when using custom paging.

Are these attributes very similar to the DataGrid properties? Yes, the DataGrid control uses the PagedDataSource class to display data paging.

Here's an example of using the PagedDataSource class to implement a paging display of the DataList and Repeater controls:
Copy Code code as follows:

public void Page_Load (Object Src,eventargs E)
{
OleDbConnection objconn=new OleDbConnection (@ "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\test.mdb ");
OleDbDataAdapter objcommand=new OleDbDataAdapter ("SELECT * from Users", objconn);
DataSet ds=new DataSet ();
Objcommand.fill (DS);

Assigning values to related properties of a PagedDataSource object
PagedDataSource Objpds = new PagedDataSource ();
Objpds.datasource = ds. Tables[0]. DefaultView;
Objpds.allowpaging = true;
Objpds.pagesize = 5;
int curpage;

The current page is fetched from the page query parameter
if (request.querystring["Page"]!= null)
Curpage=convert.toint32 (request.querystring["Page"));
Else
curpage=1;

Objpds.currentpageindex = CurPage-1;
Lblcurrentpage.text = "Page:" + curpage.tostring ();

if (!objpds.isfirstpage)
Lnkprev.navigateurl=request.currentexecutionfilepath+ "? Page= "+ onvert. ToString (CurPage-1);

if (!objpds.islastpage)
Lnknext.navigateurl=request.currentexecutionfilepath+ "? Page= "+convert.tostring (curpage+1);

Assigning the PagedDataSource object to the Repeater control
Repeater1.datasource=objpds;
Repeater1.databind ();
}

This can be simple to achieve repeater paging function, but there is a disadvantage is that every time the data query out, if the amount of data, so the efficiency will be relatively low. At this time with a stored procedure to achieve better!
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.