Implement record statistics pagination for the datalist control to count the first and last pages of the previous and next pages
Not much.
Html Code As follows:
<Asp: datalist id = "datalist1" runat = "server" datakeyfield = "zhucyhm">
<Itemtemplate>
Bianh:
<Asp: Label id = "bianhlabel" runat = "server" text = '<% # eval ("bianh") %>'> </ASP: Label> <br/>
Zhucyhm:
<Asp: Label id = "zhucyhmlabel" runat = "server" text = '<% # eval ("zhucyhm") %>'> </ASP: Label> <br/>
Xingm:
<Asp: Label id = "xingmlabel" runat = "server" text = '<% # eval ("xingm") %>'> </ASP: Label> <br/>
Xingb:
<Asp: Label id = "xingblabel" runat = "server" text = '<% # eval ("xingb") %>'> </ASP: Label> <br/>
<Br/>
</Itemtemplate>
</ASP: datalist> <asp: sqldatasource id = "sqlcece1" runat = "server" connectionstring = "<% $ connectionstrings: xbmisconnectionstring %>"
Selectcommand = "select bianh, zhucyhm, xingm, xingb from t_yongh"> </ASP: sqldatasource>
<Br/>
<Table Style = "width: 619px; Height: 75px">
<Tr>
<TD colspan = "3" rowspan = "3" style = "font-family:; width: 436px;">
Total <asp: Label id = "lblrecordcount" runat = "server"> label </ASP: Label> Records & nbsp; Total <asp: label id = "lblpagecount" runat = "server"> label </ASP: Label> the current <asp: label id = "lblcurrentpage" runat = "server"> label </ASP: Label> page & nbsp;
<Asp: linkbutton id = "lkfirstpage" runat = "server" oncommand = "page_onclick" commandname = "first"> homepage </ASP: linkbutton> & nbsp;
<Asp: linkbutton id = "lkprevpage" runat = "server" oncommand = "page_onclick" commandname = "Prev"> previous page </ASP: linkbutton> & nbsp;
<Asp: linkbutton id = "lknextpage" runat = "server" oncommand = "page_onclick" commandname = "Next"> next page </ASP: linkbutton> & nbsp;
<Asp: linkbutton id = "lklastpage" runat = "server" oncommand = "page_onclick" commandname = "last"> last page </ASP: linkbutton>
</TD>
</Tr>
</Table>
The background code is as follows:
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. sqlclient;
Public partial class datalist_exam6: system. Web. UI. Page
{
Int pagesize, recordcount, pagecount, currentpage;
Protected void page_load (Object sender, eventargs E)
{
// Number of records displayed on each page
Pagesize = 3;
If (! Page. ispostback)
{
// Calculate the total number of records
Recordcount = calculaterecord ();
// Calculate the total number of pages rounded up
Pagecount = recordcount/pagesize;
If (recordcount % pagesize> 0)
Pagecount = pagecount + 1;
Lblpagecount. Text = pagecount. tostring ();
Lblrecordcount. Text = recordcount. tostring ();
Viewstate ["pagecount"] = pagecount;
Currentpage = 0;
Viewstate ["pageindex"] = 0;
}
// Datalist binding
Datalistbind ();
}
// Count the number of records
Public int calculaterecord ()
{
// Declare a string
String sconnectionstring;
// Connect the database string to the ajaxdb database. The user name is Sa.
Sconnectionstring = "Data Source =.; initial catalog = xbmis; user id = sa ;";
// Create a sqlconnection database connection object
Sqlconnection conn = new sqlconnection (sconnectionstring );
// Enable Conn
Conn. open ();
Int intcount;
Sqlcommand comm = new sqlcommand ("select count (*) as number from t_yongh", Conn );
Sqldatareader Dr;
Dr = comm. executereader ();
If (dr. Read ())
{
Intcount = int32.parse (Dr ["Number"]. tostring ());
}
Else
{
Intcount = 0;
}
Dr. Close ();
Return intcount;
}
Public void datalistbind ()
{
// Declare a string
String sconnectionstring;
// Connect the database string to the xbmis database. The user name is Sa.
Sconnectionstring = "Data Source =.; initial catalog = xbmis; user id = sa ;";
// Create a sqlconnection database connection object
Sqlconnection conn = new sqlconnection (sconnectionstring );
// Enable Conn
Conn. open ();
String SQL;
SQL = "select bianh, zhucyhm, xingm, xingb from t_yongh ";
// Create and initialize the sqlcommand object and execute the SQL command
Sqldataadapter infor = new sqldataadapter (SQL, Conn );
Dataset DS = new dataset ();
// Fill dataset with the fill method of sqldataadapter
Infor. Fill (DS, "infor ");
// Data binding of the gridview1 Control
Datalist1.datasource = Ds. Tables ["infor"];
Datalist1.databind ();
Lbnnextpage. Enabled = true;
Lbnprevpage. Enabled = true;
If (pagecount = 0)
{
Lblcurrentpage. Text = "0 ";
Lknextpage. Enabled = false;
Lkprevpage. Enabled = false;
}
Else
{
If (currentpage = (pagecount-1) lknextpage. Enabled = false;
If (currentpage = 0) lkprevpage. Enabled = false;
Lblcurrentpage. Text = (currentpage + 1). tostring ();
}
}
Protected void page_onclick (Object sender, commandeventargs E)
{
Currentpage = (INT) viewstate ["pageindex"];
Pagecount = (INT) viewstate ["pagecount"];
String cmd = E. commandname;
// Identify cmd to determine the page turning direction
Switch (CMD)
{
Case "Next ":
If (currentpage <(pagecount-1) currentpage ++;
Break;
Case "Prev ":
If (currentpage> 0) currentpage --;
Break;
Case "first ":
Currentpage = 0;
Break;
Case "last ":
Currentpage = pagecount-1;
Break;
}
Viewstate ["pageindex"] = currentpage;
Datalistbind ();
}
}