The pagination of datalist and repeater is required for the project. However, the control for loading data does not provide the paging function itself. You need to control the query statement for paging, but the statement is too complicated, or there are too many conditions, and the statement is difficult to write, but pageddatasource is much simpler, and the code distribution is clear and easy to understand.
The following example uses the pageddatasource class to implement the data paging of the datalist control, which can also be used for repeater.
Page background code
Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data;
Using system. Data. sqlclient;
Public partial class Demo: system. Web. UI. Page
{
Protected sqlconnection conn; // Add a database operation object
Protected sqldataadapter da;
Protected dataset Ds;
Protected sqlcommand comm;
Protected void page_load (Object sender, eventargs E)
{
Getarticle ();
}
Private void getarticle () // get Article data
{
Conn = new sqlconnection ("Server = 127.0.0.1; database = obtainemployment; user id = sa; Password =;"); // obtain the connection string to establish a connection
DA = new sqldataadapter ();
Da. selectcommand = new sqlcommand ("select top 50 * From db_article where checkup = '1' order by intime DESC", Conn );
DS = new dataset ();
Try
{
Conn. open ();
Da. Fill (DS, "article ");
Conn. Close ();
}
Catch (sqlexception E1)
{
Response. Write (e1.tostring ());
}
Int cup = convert. toint32 (this. lb_currentpage.text); // the current page number, initialized to the ground 1 page
Pageddatasource PS = new pageddatasource ();
PS. datasource = Ds. Tables ["article"]. defaultview;
PS. allowpaging = true;
PS. pagesize = 6; // The number of rows per page
PS. currentpageindex = Cup-1;
Lb_count.text = ps. performancecount. tostring (); // obtain the total number of records
Lb_page.text = ps. pagecount. tostring (); // obtain the total number of pages
If (! Ispostback)
{
For (INT I = 1; I <ps. pagecount + 1; I ++)
{
This. dropdownlist1.items. Add (I. tostring ());
}
Linkup. Enabled = true;
Linkdown. Enabled = true;
}
Try
{
Dropdownlist1.selecteditem. Text = Cup. tostring ();
Datalist1.datasource = Ps;
Datalist1.databind ();
}
Catch (exception ex)
{
Throw ex;
}
}
Protected void linkdown_click (Object sender, eventargs e) // code of the next page button
{
Try
{
Lb_currentpage.text = convert. tostring (convert. toint32 (lb_currentpage.text) + 1 );
Dropdownlist1.selectedvalue = lb_currentpage.text;
Getarticle ();
}
Catch (exception ex)
{
Response. Write ("<script language = JavaScript>" + "alert (\" is the last page \ ")" + "</SCRIPT> ");
Lb_currentpage.text = "1 ";
Getarticle ();
}
}
Protected void dropdownlist1_selectedindexchanged (Object sender, eventargs e) // code to jump to the specified page
{
Int page = convert. toint16 (dropdownlist1.selecteditem. Value ));
Lb_currentpage.text = page. tostring ();
Getarticle ();
}
Protected void linkup_click (Object sender, eventargs e) // code of the previous page button
{
Try
{
If (convert. toint16 (lb_currentpage.text)> 1)
{
Lb_currentpage.text = convert. tostring (convert. toint32 (lb_currentpage.text)-1 );
Dropdownlist1.selectedvalue = lb_currentpage.text;
Getarticle ();
}
Else
{
Response. Write ("<script language = JavaScript>" + "alert (\" already the first page \ ")" + "</SCRIPT> ");
}
}
Catch (exception ex)
{
Response. Write ("<script language = JavaScript>" + "alert (\" already the first page \ ")" + "</SCRIPT> ");
}
}
Protected void linkfirst_click (Object sender, eventargs e) // jump to the first page of code
{
If (lb_currentpage.text! = "1 ")
Lb_currentpage.text = "1 ";
Else
{
Response. Write ("<script language = JavaScript>" + "alert (\" already the first page \ ")" + "</SCRIPT> ");
}
Getarticle ();
}
Protected void linklast_click (Object sender, eventargs e) // jump to the Code on the last page
{
If (lb_currentpage.text.tostring ()! = Lb_page.text.tostring ())
Lb_currentpage.text = lb_page.text.tostring ();
Else
{
Response. Write ("<script language = JavaScript>" + "alert (\" is the last page \ ")" + "</SCRIPT> ");
}
Getarticle ();
}
}
Page front-end code
<% @ Page Language = "C #" codefile = "demo. aspx. cs" inherits = "Demo" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: datalist id = "datalist1" runat = "server">
<Itemtemplate>
<Asp: Label id = "lbnwes" runat = "server" text = '<% # eval_r ("title") %>'> </ASP: Label>
& Nbsp;
<Asp: Label id = "lbtime" runat = "server" text = '<% # eval_r ("intime") %>'> </ASP: Label>
</Itemtemplate>
</ASP: datalist> </div>
<Br/>
A total of <asp: Label id = "lb_count" runat = "server" text = "label"> </ASP: Label> records
& Nbsp; Total <asp: Label id = "lb_page" runat = "server" text = "label"> </ASP: Label> page & nbsp;
& Nbsp; current <asp: Label id = "lb_currentpage" runat = "server" text = "1"> </ASP: Label> page & nbsp;
& Nbsp;
<Asp: linkbutton id = "linkfirst" runat = "server" onclick = "linkfirst_click"> page 1 </ASP: linkbutton>
& Nbsp;
<Asp: linkbutton id = "linkup" runat = "server" onclick = "linkup_click"> previous page </ASP: linkbutton>
& Nbsp; <asp: linkbutton id = "linkdown" runat = "server" onclick = "linkdown_click"> next page </ASP: linkbutton>
& Nbsp; <asp: linkbutton id = "linklast" runat = "server" onclick = "linklast_click"> last page </ASP: linkbutton>
& Nbsp; go to the <asp: dropdownlist id = "dropdownlist1" runat = "server" onselectedindexchanged = "dropdownlist1_selectedindexchanged">
</ASP: dropdownlist> page
</Form>
</Body>
</Html>
Note: Some common 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 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.