A few days ago, when I was doing an online store, I wanted to display the product information (including images and texts). Although dataview can be paged, its disadvantage was that it could not customize the display format. Datalist solves its shortcomings, but datalist itself cannot be paged. It was a headache, so I found some information on the Internet and solved the problem. Now I will share with you the implementation process:
1. Write by yourselfCode(Custom control) 2. Use a third-party control (such as aspnetpager)
I will share my custom controls,
The core principle of my datalist paging method is to use the pageddatasource object. The pageddatasource class encapsulates the properties of the DataGrid control. These attributes enable the DataGrid to execute pagination. below is the public attribute of pageddatasource:
Allowcustompaging gets or sets the value indicating whether to enable custom paging.
Allowpaging gets or sets the value indicating whether to enable pagination.
Count to obtain the number of items to be used from the data source.
Currentpageindex gets or sets the index of the current page.
Datasource obtains or sets the data source.
Cececount gets the number of items in the data source.
Firstindexinpage gets 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 obtains the total number of pages required for all items in the data source.
Pagesize gets or sets the number of items to be displayed on a single page.
Virtualcount gets or sets the actual number of items in the data source when custom pages are used.
Step 1: extract the data to the able, obtain the dataview, and pay it to the pageddatasource object.
Dataview objview = objtable. defaultview;
Pageddatasource objpds = new pageddatasource ();
Objpds. datasource = objview;
Step 2: configure the object objpds of pageddatasource
Objpds. allowpaging = true;
Objpds. pagesize = 4;
Objpds. currentpageindex = int. parse (viewstate ["pageindex"]. tostring ());
The above two pieces of code are written in the binddata () function for each call when you click the paging button. The binddata () function is as follows:
Private void binddata ()
{
Books book = new books (); // books is a class used to operate a database table.
Datatable objtable = book. getnewbooks (24); // There is a function in the book. The returned result is of the datatable type. You can modify the code here as needed.
If (objtable! = NULL & objtable. Rows. Count> 0)
{
Dataview objview = objtable. defaultview;
Pageddatasource objpds = new pageddatasource ();
Objpds. datasource = objview;
Objpds. allowpaging = true;
Objpds. pagesize = 4;
Objpds. currentpageindex = int. parse (viewstate ["pageindex"]. tostring ());
If (! Objpps. isfirstpage)
{
Lkpre. Visible = true;
}
Else
{
Lkpre. Visible = false;
}
If (! Objpps. islastpage)
{
Lknext. Visible = true;
}
Else
{
Lknext. Visible = false;
}
Dldata. datasource = objpds;
Dldata. databind ();
}
}
In the third line of code above, viewstate ["pageindex"] is used to control the current page of pageddatasource, in the page_load event and the function to be added in step 3, we will explain how to use viewstate ["pageindex"] to control the current page.
Step 3: Page Control
In step 2, viewstate ["pageindex"] is used to control the current page. to display the first page when the page is loaded, of course, we need to set viewstate ["pageindex"] to 0 in the page_load event.
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Viewstate ["pageindex"] = "0 ";
Binddata ();}
}
We need to add two linkbuttons (previous and next) for paging control.
<Asp: datalist id = "dldata" runat = "server" repeatcolumns = "4">
<Itemtemplate>
<Asp: Label id = "lblname" runat = "server"/> <br/>
<Asp: Label id = "lbltime" runat = "server"/>
</Itemtemplate>
</ASP: datalist>
<Asp: linkbutton id = "lkpre" oncommand = "indexchanging" commandargument = "pre" runat = "server"> Previous Page> </ASP: linkbutton>
<Asp: linkbutton id = "lknext" oncommand = "indexchanging" commandargument = "Next" runat = "server"> next page </ASP: linkbutton>
The oncommand of both buttons is the indexchanging () function. We can use commandargument (Pre and next) to identify whether to flip forward or backward. The following is the indexchanging () function.
Protected void indexchanging (Object sender, eventargs E)
{
String strcommand = (linkbutton) sender). commandargument. tostring ();
Int pageindex = int. parse (viewstate ["pageindex"]. tostring ());
If (strcommand = "pre ")
{
Pageindex = pageindex-1;
}
Else
{
Pageindex = pageindex + 1;
}
Viewstate ["pageindex"] = pageindex;
Binddata ();
}
So far, our paging function has been completed.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/mane_yao/archive/2010/03/22/5403599.aspx