Pagination implementation of the Repeater control and pagination of the repeater control
This article describes how to use the Repeater control and PagedDataSource to implement its paging function. The PagedDataSource class encapsulates the attributes that allow data source controls (such as DataGrid and GridView) to perform paging operations. This type can be used if control developers need to provide paging support for custom data binding controls.
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 // obtain the number of items to be used from the data source. CurrentPageIndex // obtain or set the index of the current page. DataSource // obtain or set the data source. Performancecount // obtain the number of items in the data source. FirstIndexInPage // obtain 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 to synchronize access to the data source (thread safety ). PageCount // obtain the total number of pages required to display all items in the data source. PageSize // obtain or set the number of items to be displayed on a single page. VirtualCount // obtain or set the actual number of items in the data source when custom pages are used.
The following is an example of PagedDataSource control display by page,
This example is written in vs.net 2008 (C. Background. CS code. From admin).com
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
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 the data source pds.AllowPaging = true ; // Set the value indicating whether paging is enabled pds.PageSize = 5; // Set the number of entries to be displayed on each page pds.CurrentPageIndex = pageIndex - 1; // Set the index of the current page. rptDocumentList.DataSource = pds; rptDocumentList.DataBind(); ltlPageBar.Text = GetPageBar(pds); } } // Pagination bar private string GetPageBar(PagedDataSource pds) { string pageBar = string .Empty; int currentPageIndex = pds.CurrentPageIndex + 1; if (currentPageIndex == 1) { pageBar += "<A href = \" javascript: void (0) \ "> homepage </a>" ; } else { pageBar += "<a href=\"" + Request.CurrentExecutionFilePath + "? Page = 1 \ "> homepage </a>" ; } if ((currentPageIndex - 1) < 1) { pageBar += "<A href = \" javascript: void (0) \ "> previous page </a>" ; } else { pageBar += "<a href=\"" + Request.CurrentExecutionFilePath + "?Page=" + (currentPageIndex - 1) + "\"> Previous page </a>" ; } if ((currentPageIndex + 1) > pds.PageCount) { pageBar += "<A href = \" javascript: void (0) \ "> next page </a>" ; } else { pageBar += "<a href=\"" + Request.CurrentExecutionFilePath + "?Page=" + (currentPageIndex + 1) + "\"> Next page </a>" ; } if (currentPageIndex == pds.PageCount) { pageBar += "<A href = \" javascript: void (0) \ "> last page </a>" ; } else { pageBar += "<a href=\"" + Request.CurrentExecutionFilePath + "?Page=" + pds.PageCount + "\"> Last page </a>" ; } return pageBar; } // Create a test table 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; } |
Foreground. aspx code
12345678910111213141516171819 |
< form id = "form1" runat = "server" > < div > < asp:Repeater ID = "rptDocumentList" runat = "server" > < HeaderTemplate > < ul > </ HeaderTemplate > < ItemTemplate > < li > <%# DataBinder.Eval(Container.DataItem, "Title")%></ li > </ ItemTemplate > < FooterTemplate > </ ul > </ FooterTemplate > </ asp:Repeater > </ div > < div class = "pageBar" > < asp:Literal ID = "ltlPageBar" runat = "server" ></ asp:Literal > </ div > </ form > |
Download code example: PageDemo. RAR