Use DataList to implement data paging.
When I was doing my website today, I used the paging technology to record the usage methods for future reference and help new friends.
The DataList control can display multiple row records in a data table in the form of a list. However, the displayed multi-row records do not have the paging function, which is inconvenient to use. ThereforePagedDataSource classThis class encapsulates the paging attributes of the Data Control. Its common attributes and descriptions are shown in the following table.
| Genus |
Description |
| AllowPaging |
Get or set whether to enable Paging |
| AllowCustomPaging |
Gets or sets whether to enable custom paging.
|
| CurrentPageIndex |
Obtains or sets the index of the current display page.
|
DataSource
|
Obtains or sets the source data used to fill in items in the control.
|
PageSize
|
Gets or sets the number of items to be displayed on each page of the data-bound control.
|
| PageCount |
Obtain the total number of pages required for each item in the display data binding control. |
FirstIndexPage
|
Obtain the first index on the page
|
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 |
| DataKeyField |
Obtains or sets the key field in the data source specified by the DataSource attribute.
|
DataKeys
|
Obtains the key value of each record stored in the data list control.
|
The general implementation is as follows:
The Code is as follows:
Front-end (aspx) code:
<Div class = "you_align"> <table style = "width: export PX "cellpadding =" 0 "cellspacing =" 0 "align =" center "> <tr> <td> & nbsp; </td> </tr> <td style = "width: 600px; text-align: align; font-size: 9pt; height: 15px; "> <asp: Label ID =" labCP "runat =" server "Text =" current "> </asp: Label> <asp: label ID = "labPage" runat = "server" Text = "1"> </asp: Label> & nbsp; page <asp: label ID = "labTP" runat = "server" Text = ""> </asp: Label> <asp: label ID = "labBackPage" runat = "server"> </asp: Label> page <asp: linkButton ID = "lnkbtnOne" runat = "server" Font-Underline = "False" ForeColor = "Red" OnClick = "lnkbtnOne_Click"> page 1 </asp: LinkButton> <asp: linkButton ID = "lnkbtnUp" runat = "server" Font-Underline = "False" ForeColor = "Red" OnClick = "lnkbtnUp_Click"> previous page </asp: LinkButton> <asp: linkButton ID = "lnkbtnNext" runat = "server" Font-Underline = "False" ForeColor = "Red" OnClick = "lnkbtnNext_Click"> next page </asp: LinkButton> & nbsp; <asp: LinkButton ID = "lnkbtnBack" runat = "server" Font-Underline = "False" ForeColor = "Red" OnClick = "lnkbtnBack_Click"> last page </asp: linkButton> & nbsp; </td> </tr> </table> </div>
The backend aspx. cs code is as follows:
Using System; using System. collections; using System. configuration; using System. data; using System. linq; using System. web; using System. web. security; using System. web. UI; using System. web. UI. htmlControls; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. xml. linq; public partial class zwgk: System. web. UI. page {CommonClass CC = new CommonClass (); protected void Page_Load (Object sender, EventArgs e) {bind (); // ZWGK for government affairs. dataSource = CC. getDataSet ("SELECT * FROM News WHERE Style = 'government affairs public' order by Time Desc", "News"); ZWGK. dataKeyField = "id"; ZWGK. dataBind ();} protected void bind () {// retrieve the current page number int curpage = Convert. toInt32 (this. labPage. text); // use the PagedDataSource class to implement the paging function of the DataList control. PagedDataSource ps = new PagedDataSource (); // obtain the DataSet ds = CC. getDataSet ("sele Ct * from News where style = 'government affairs public' order by Time Desc "," News "); ps. dataSource = ds. tables ["News"]. defaultView; // whether pagination is allowed. allowPaging = true; // Number of displayed ps. pageSize = 15; // retrieve the current page number ps. currentPageIndex = curpage-1; this. lnkbtnUp. enabled = true; this. lnkbtnNext. enabled = true; this. lnkbtnBack. enabled = true; this. lnkbtnOne. enabled = true; if (curpage = 1) {// The first page button is not displayed this. lnkbtnOne. enabled = fals E; // do not display the previous page button this. lnkbtnUp. enabled = false;} if (curpage = ps. pageCount) {// do not display the next page this. lnkbtnNext. enabled = false; // The last page is not displayed. lnkbtnBack. enabled = false;} // display the number of pages this. labBackPage. text = Convert. toString (ps. pageCount); // bind the DataList control this. ZWGK. dataSource = ps; this. ZWGK. dataKeyField = "id"; this. ZWGK. dataBind ();} // protected void lnkbtnOne_Click (object sender, EventArgs e) {this. l AbPage. text = "1"; this. bind ();} // previous page protected void lnkbtnUp_Click (object sender, EventArgs e) {this. labPage. text = Convert. toString (Convert. toInt32 (this. labPage. text)-1); this. bind ();} // protected void lnkbtnNext_Click (object sender, EventArgs e) {this. labPage. text = Convert. toString (Convert. toInt32 (this. labPage. text) + 1); this. bind ();} // protected void lnkbtnBack_Click (object Sender, EventArgs e) {this. labPage. text = this. labBackPage. text; this. bind ();} protected void ZWGK_ItemCommand (object source, DataListCommandEventArgs e) {int id = Convert. toInt32 (ZWGK. dataKeys [e. item. itemIndex]. toString (); Response. write ("<script language = javascript> location = 'show _ News. aspx? Id = "+ id +" '</script> ");}}