Datalist page
First Note: not all of them are original, most of them refer to others'CodeI just fixed some bugs.
Bytes --------------------------------------------------------------------------------------------------------
Front-end code:
------
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "test. aspx. cs" inherits = "news_test" %>
<! 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>
<Table id = "Table1" style = "Z-INDEX: 101; left: 32px; width: 752px; position: absolute; top: 16px; height: 312px "cellspacing =" 0 "cellpadding =" 0 "width =" 752 "border =" 0 ">
<Tr>
<TD style = "height: 29px"> <font face = ""> datalist paging technology and hyperlink </font> </TD>
</Tr>
<Tr>
<TD style = "height: 252px">
<Asp: datalist id = "datalist1" runat = "server" width = "576px" Height = "96px">
<Headertemplate>
Order No. <TD>
Employee ID <TD>
Order Date <TD>
Freight <TD>
Shipping to the city
</Headertemplate>
<Itemtemplate>
<% # Databinder. eval (container. dataitem, "orderid") %> <TD>
<% # Databinder. eval (container. dataitem, "customerid") %> <TD>
<% # Databinder. eval (container. dataitem, "orderdate") %> <TD>
<% # Databinder. eval (container. dataitem, "freight") %> <TD>
<% # Databinder. eval (container. dataitem, "shipcity") %>
</Itemtemplate>
</ASP: datalist>
</TD>
</Tr>
<Tr>
<TD> <font face = "">
<Asp: linkbutton id = "firstlb" runat = "server" oncommand = "linkbutton_click" commandname = "first"> page 1 </ASP: linkbutton> & nbsp;
<Asp: linkbutton id = "previouslb" runat = "server" oncommand = "linkbutton_click" commandname = "Prev"> previous page </ASP: linkbutton> & nbsp;
<Asp: linkbutton id = "nextlb" runat = "server" oncommand = linkbutton_click commandname = "Next"> next page </ASP: linkbutton> & nbsp;
<Asp: linkbutton id = "endlb" runat = "server" oncommand = linkbutton_click commandname = "end"> last page </ASP: linkbutton> & nbsp;
Total <asp: Label id = "totallbl" runat = "server"> </ASP: Label> page current section <asp: label id = "currentlbl" runat = "server"> </ASP: Label> page
<Asp: linkbutton id = "jumplb" runat = "server" oncommand = linkbutton_click commandname = "jump"> skip to </ASP: linkbutton>
<Asp: textbox id = "textbox1" runat = "server" width = "90px"> </ASP: textbox>
Page </font> </TD>
</Tr>
</Table>
</Div>
</Form>
</Body>
</Html>
-------------------------------------------------
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. sqlclient;
Public partial class news_test: system. Web. UI. Page
{
Int currentpage; // the current page number.
Int pagesize; // number of entries per page
Int pagecount; // the total number of pages.
Int recordcount; // The total number of records.
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
Pagesize = 10; // 10 records per page
If (! Page. ispostback)
{
Currentpage = 0; // the current page is set to 0
Viewstate ["pageindex"] = 0; // The page index is also set to 0
// Calculate the total number of records
Recordcount = calculaterecord ();
// calculate the total number of pages
If (recordcount % pagesize = 0)
{< br> pagecount = recordcount/pagesize;
}< br> else
{< br> pagecount = recordcount/pagesize + 1;
}
This. totallbl. Text = pagecount. tostring (); // displays the total number of pages.
Viewstate ["pagecount"] = pagecount; // The session is valid for the entire application, while the viewstate is equivalent to the session of a page.
This. datalistbind (); // It cannot be bound before the initialization condition. In this case, if there is only one page of data, the next page is still displayed.
}
}
// Calculate the total number of records
Private int calculaterecord ()
{
Try
{
Int recordcount;
Sqlconnection con = new sqlconnection ("Server = 127.0.0.1; database = northwind; uid = sa; Pwd = sa"); // The database uses northwind;
Con. open ();
String SQL = "select count (*) as count from orders ";
Sqlcommand cmd = new sqlcommand (SQL, con );
Sqldatareader SDR = cmd. executereader ();
If (SDR. Read ())
{
Recordcount = int32.parse (SDR ["count"]. tostring ());
}
Else
{
Recordcount = 0;
}
SDR. Close ();
Con. Close ();
Return recordcount;
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
}
// Bind data to the datalist Control
Public void datalistbind ()
{
Try
{
Int startindex = currentpage * pagesize; // you can specify the start and end addresses of the import.
String SQL = "select * from orders ";
Dataset DS = new dataset ();
Sqlconnection con = new sqlconnection ("Server = 127.0.0.1; database = northwind; uid = sa; Pwd = sa ");
Con. open ();
Sqldataadapter SDA = new sqldataadapter (SQL, con );
SDA. fill (DS, startindex, pagesize, "orders"); // This is SDA. the first overload of the fill method. The variables in the method are dataset, which starts to record startrecord, maxrecord, and tablename.
This. datalist1.datasource = Ds. Tables ["orders"]. defaultview;
This. datalist1.databind ();
This. previuslb. Enabled = true;
This. nextlb. Enabled = true;
If (currentpage = (pagecount-1) This. nextlb. Enabled = false; // when the last page is displayed, the next page link button is unavailable.
If (currentpage = 0) This. previuslb. Enabled = false; // when the first page is displayed, the previous page button is unavailable.
This. currentlbl. Text = (currentpage + 1). tostring (); // current page number
}
Catch (exception ex)
{
Throw new exception (ex. Message );
}
}
Public void linkbutton_click (Object sender, commandeventargs e) // click the event in a self-compiled button
{
Currentpage = (INT) viewstate ["pageindex"]; // obtain the index of the current page
Pagecount = (INT) viewstate ["pagecount"]; // obtain the total number of pages
String cmd = E. commandname;
// Identify cmd to determine the page turning direction
Switch (CMD)
{
Case "Prev": // Previous Page
If (currentpage> 0) currentpage --;
Break;
Case "Next ":
If (currentpage <(pagecount-1) currentpage ++; // next page
Break;
Case "first": // page 1
Currentpage = 0;
Break;
Case "end": // last page
Currentpage = pagecount-1;
Break;
Case "jump": // page to jump
If (this. textbox1.text. Trim () = "" | int32.parse (this. textbox1.text. Trim ()> pagecount) // If the input number is null or beyond the range, return
{
Return;
}
Else
{
Currentpage = int32.parse (this. textbox1.text. tostring ()-1;
Break;
}
}
Viewstate ["pageindex"] = currentpage; // obtain the current page
This. datalistbind (); // re-bind datalist to the database
}
}
------------------------
In actual use, you need to verify the positive integer of the input value in the text box that is redirected.