Repeater controls combine with PagedDataSource to achieve paging

  • 2020-10-07 18:38:02
  • OfStack

This article shows how Repeater controls combine with PagedDataSource to achieve its paging capability. The PagedDataSource class encapsulates properties that allow data source controls such as DataGrid and GridView to perform paging operations. You can use this class if control developers need paging support for custom data-bound controls.

Some common properties of the PagedDataSource class:

AllowCustomPaging // gets or sets the 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 one. 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 access to the data source is synchronized (thread-safe). PageCount // Gets the total number of pages needed 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.
The following is a pagination display example of Repeater control implemented by PagedDataSource class, as shown in the figure below:

 
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!Page.IsPostBack) 
{ 
int pageIndex = 1; 
try 
{ 
pageIndex = Convert.ToInt32(Request.QueryString["Page"]); 
if (pageIndex <= 0) pageIndex = 1; 
} 
catch 
{ 
pageIndex = 1; 
} 
DataTable dt = GetDocumentTable(); 
PagedDataSource pds = new PagedDataSource(); 
pds.DataSource = dt.DefaultView; //  Set up the data source  
pds.AllowPaging = true; //  Sets the value indicating whether paging is enabled  
pds.PageSize = 5; //  Sets the amount to be displayed per page  
pds.CurrentPageIndex = pageIndex - 1; //  Sets the index of the current page.  
rptDocumentList.DataSource = pds; 
rptDocumentList.DataBind(); 
ltlPageBar.Text = GetPageBar(pds); 
} 
} 
//  Article page  
private string GetPageBar(PagedDataSource pds) 
{ 
string pageBar = string.Empty; 
int currentPageIndex = pds.CurrentPageIndex + 1; 
if (currentPageIndex == 1) 
{ 
pageBar += " Home page "; 
} 
else 
{ 
pageBar += " + Request.CurrentExecutionFilePath + "?Page=1"> Home page "; 
} 
if ((currentPageIndex - 1) < 1) 
{ 
pageBar += " on 1 page "; 
} 
else 
{ 
pageBar += " + Request.CurrentExecutionFilePath + "?Page=" + (currentPageIndex - 1) + ""> on 1 page "; 
} 
if ((currentPageIndex + 1) > pds.PageCount) 
{ 
pageBar += " Under the 1 page "; 
} 
else 
{ 
pageBar += " + Request.CurrentExecutionFilePath + "?Page=" + (currentPageIndex + 1) + ""> Under the 1 page "; 
} 
if (currentPageIndex == pds.PageCount) 
{ 
pageBar += " At the end of the page "; 
} 
else 
{ 
pageBar += " + Request.CurrentExecutionFilePath + "?Page=" + pds.PageCount + ""> At the end of the page "; 
} 
return pageBar; 
} 
//  Create test tables  
DataTable GetDocumentTable() 
{ 
DataTable dt = new DataTable(); 
dt.Columns.Add("DocumentId", typeof(int)); 
dt.Columns.Add("Title", typeof(string)); 
for (int i = 1; i <= 30; i++) 
{ 
DataRow row = dt.NewRow(); 
row["DocumentId"] = i; 
row["Title"] = " Document title  " + i + ""; 
dt.Rows.Add(row); 
} 
return dt; 
} 


Related articles: