The use of asp.net Repeater paging instances of PageDataSource

  • 2020-06-01 09:23:21
  • OfStack

Asp.net provides three powerful list controls: DataGrid, DataList, and Repeater, but only the DataGrid controls provide pagination. The DataGrid, DataList, and Repeater controls have a higher degree of style customization than the DataGrid, DataList, and Repeater controls, so many times we prefer to use the DataList or Repeater controls to display data.

There are several ways to implement the paging display of DataList or Repeater controls:
1. Write a method or stored procedure that returns the data table to be displayed based on the number of pages passed in (DataTable)
2, use PagedDataSource class (located in System. Web. UI. WebControls namespaces)

This article focuses on how to use the PagedDataSource class to implement the paging display of the 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 that enable DataGrid to perform paging.

Some of the public properties of the PagedDataSource class:
AllowCustomPaging gets or sets a value indicating whether custom paging is enabled.
AllowPaging gets or sets a value indicating 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 on the page.
IsCustomPagingEnabled gets a value indicating whether custom paging is enabled.
IsFirstPage gets a value indicating whether the current page is the first page.
IsLastPage gets a value indicating whether the current page is the last page.
IsPagingEnabled gets a value indicating whether paging is enabled.
IsReadOnly gets a value indicating whether the data source is read-only.
IsSynchronized gets a value indicating whether to synchronize access to the data source (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 actual number of items in the data source when custom paging is used.

Are these properties similar to those of DataGrid? Yes, the DataGrid control USES the PagedDataSource class for pagination.

Here is an example of the paging display of DataList and Repeater controls using the PagedDataSource class:

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

// right PagedDataSource  Object's associated property assignment  
PagedDataSource objPds = new PagedDataSource(); 
objPds.DataSource = ds.Tables[0].DefaultView; 
objPds.AllowPaging = true; 
objPds.PageSize = 5; 
int CurPage; 

// Current page from Page Query parameter acquisition  
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); 

// the PagedDataSource  The object is assigned to Repeater controls  
Repeater1.DataSource=objPds; 
Repeater1.DataBind(); 
}

This can easily implement the paging function of Repeater, but it has the disadvantage of querying all the data at once. If the data volume is large, the efficiency will be low. It is better to use stored procedures at this point!

Related articles: