Http://www.cnblogs.com/PLAYBOY840616/archive/2006/12/29/607482.html
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)
{
PageCount = RecordCount/PageSize;
}
Else
{
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
}
}