Using the PagedDataSource class to implement the paging display of DataList and Repeater controls

Source: Internet
Author: User
Pagination | controls | display

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, the use of PagedDataSource class

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 custom paging is enabled.
 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   Get or set the data source.
 DataSourceCount   Gets the number of items in the data source. The
 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 first.
 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 is synchronized (thread-safe).
 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:

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= "+ convert.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 makes it easy to implement the paging display of the DataList and Repeater controls. But there is a drawback is that every time you have to select all the pages of data, the DataGrid is the case, this will reduce the efficiency (most of the time do not see the difference); If you use the first method you can select only the data of the current page (Implementation method please view the 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.