Implement using the PagedDataSource class
Pagination of DataList and Repeater controls
Asp.net provides three powerful list controls: the DataGrid, DataList, and Repeater controls, which have higher style customization compared with the DataGrid, DataList, and Repeater controls, many times we like to use the DataList or Repeater control to display data. However, Repeater and DataList do not have the paging function and are sometimes inconvenient.
The PagedDataSource class encapsulates the properties of the DataGrid control so that the DataGrid Control can execute pagination. It is a data container. We first read the data from the database and put it in this container, then, set the container property to retrieve part of the data on the page to be displayed, and then bind the data to the display control on the page.
The following example shows the data paging list implemented by the Repeater control and HyperLink control combined with the PagedDataSource class.
Source code for data function implementation:
// Reference the class used for paging
PagedDataSource PPS = new PagedDataSource ();
Pds. DataSource = dt. DefaultView; // sets the data source (DataTable type)
PPS. AllowPaging = true;
// Number of lines displayed on the page
PPS. PageSize = 18;
// Set the current page
If (Pageindex <1) Pageindex = 1;
PPS. CurrentPageIndex = Pageindex-1;
Rpt_NewsList.DataSource = pds;
Rpt_NewsList.DataBind ();
// Display the page number
Ltl_RecordCount.Text = PPS. performancecount. ToString ();
Ltl_PageCount.Text = maid. PageCount. ToString ();
Ltl_Pageindex.Text = Pageindex. ToString ();
Ltl_Jump.Text = Jump_List (PPS. PageCount, Pageindex, L_Manage );
// Display up and down pages (the parameters following the URL are defined as needed)
Lbn_First.ToolTip = "Jump to homepage ";
Lbn_First.NavigateUrl = Request. CurrentExecutionFilePath+ "? Org_ID = "+ L_Manage +" & page = 1 ";
Lbn_Prev.ToolTip = "Jump to previous page ";
Lbn_Prev.NavigateUrl = Request. CurrentExecutionFilePath+ "? Org_ID = "+ L_Manage +" & page = "+ (Pageindex-1 );
Lbn_Next.ToolTip = "Jump to next page ";
Lbn_Next.NavigateUrl = Request. CurrentExecutionFilePath+ "? Org_ID = "+ L_Manage +" & page = "+ (Pageindex + 1 );
Lbn_Last.ToolTip = "Jump to the last page ";
Lbn_Last.NavigateUrl = Request. CurrentExecutionFilePath+ "? Org_ID = "+ L_Manage +" & page = "+ pds. PageCount. ToString ();
// Determine the link Display Mode
If (Pageindex <= 1 & tp.pagecount <= 1)
{
Lbn_First.NavigateUrl = "";
Lbn_Prev.NavigateUrl = "";
Lbn_Next.NavigateUrl = "";
Lbn_Last.NavigateUrl = "";
}
If (Pageindex <= 1 & tp.pagecount> 1)
{
Lbn_First.NavigateUrl = "";
Lbn_Prev.NavigateUrl = "";
}
If (Pageindex> = maid. PageCount)
{
Lbn_Next.NavigateUrl = "";
Lbn_Last.NavigateUrl = "";
}
/// <Summary>
/// Calculate the page Jump
/// </Summary>
/// <Param name = "Pagecount"> page count </param>
/// <Returns> string </returns>
Private string Jump_List (int Pagecount, int Pageindex, long L_Manage)
{
StringBuilder sb = new StringBuilder ();
Sb. Append ("<select id = \" Page_Jump \ "name = \" Page_Jump \ "onchange = \" window. location = '"+ Request. CurrentExecutionFilePath+ "? Page = '+ this. options [this. selectedIndex]. value +' & Org_ID = "+ L_Manage +" '; \ "> ");
For (int I = 1; I <= Pagecount; I ++)
{
If (Pageindex = I)
Sb. Append ("<option value = '" + I + "'selected>" + I + "</option> ");
Else
Sb. Append ("<option value = '" + I + "'>" + I + "</option> ");
}
Sb. Append ("</select> ");
Return sb. ToString ();
}
Dynamic Display
Some public attributes of the PagedDataSource class (omitted)