Implement pagination of DataList and Repeater controls

Source: Internet
Author: User

Asp.net provides three powerful list controls: DataGrid, DataList, and Repeater. However, only the DataGrid Control provides the paging function. Compared with the DataGrid, DataList and Repeater controls have higher style customization. Therefore, we prefer to use DataList or Repeater controls to display data.

The DataList or Repeater control can be displayed on multiple pages:
1. Write a method or stored procedure and return the data table able to be displayed based on the number of incoming pages)
2. Use the PagedDataSource class

This article describes how to use the PagedDataSource class to display DataList and Repeater controls by page. The PagedDataSource class is also used inside the DataGrid Control. The PagedDataSource class encapsulates the properties of the DataGrid control. These properties enable the DataGrid to execute pagination.

Some public attributes of the PagedDataSource class:

AllowCustomPaging gets or sets the value indicating whether to enable custom paging.
AllowPaging gets or sets the value indicating whether to enable pagination.
Count to obtain the number of items to be used from the data source.
CurrentPageIndex gets or sets the index of the current page.
DataSource obtains or sets the data source.
Cececount gets the number of items in the data source.
FirstIndexInPage gets the first index on the page.
IsCustomPagingEnabled gets a value indicating whether to enable custom paging.
IsFirstPage gets a value indicating whether the current page is a homepage.
IsLastPage gets a value indicating whether the current page is the last page.
IsPagingEnabled gets a value indicating whether to enable paging.
IsReadOnly gets a value indicating whether the data source is read-only.
IsSynchronized gets a value indicating whether the data source access thread is synchronized securely ).
PageCount obtains the total number of pages required for all items in the data source.
PageSize gets or sets the number of items to be displayed on a single page.
VirtualCount gets or sets the actual number of items in the data source when custom pages are used.

Are these attributes similar to those of the DataGrid? Yes, the DataGrid control uses the PagedDataSource class to display data by page. The following example shows how to use the PagedDataSource class to display DataList and Repeater controls by page:

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 );

// Assign values to relevant attributes of the PagedDataSource object
PagedDataSource objPds = new PagedDataSource ();
ObjPds. DataSource = ds. Tables [0]. DefaultView;
ObjPds. AllowPaging = true;
Objpps. PageSize = 5;
Int CurPage;

// Obtain the query parameters from the Page on the current Page
If (Request. QueryString ["Page"]! = Null)
CurPage = Convert. ToInt32 (Request. QueryString ["Page"]);
Else
CurPage = 1;

ObjPds. CurrentPageIndex = CurPage-1;
LblCurrentPage. Text = "Page:" + CurPage. ToString ();

If (! Objpps. IsFirstPage)
LnkPrev. NavigateUrl = Request. CurrentExecutionFilePath + "? Page = "+

Convert. ToString (CurPage-1 );

If (! Objpps. IsLastPage)
LnkNext. NavigateUrl = Request. CurrentExecutionFilePath + "? Page = "+

Convert. ToString (CurPage + 1 );

// Assign the PagedDataSource object to the Repeater control
Repeater1.DataSource = objPds;
Repeater1.DataBind ();
}

In this way, the DataList and Repeater controls can be easily displayed on pages. However, the disadvantage of this operation is that the data on all pages should be selected each time, and the DataGrid should be the same. This will reduce the efficiency and most of the time it will not be different ); if you use the first method, you can Select only the data on the current page. For more information, see the relevant article ).


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.