The following code is a simple example of Ajax paging.
View examples
The Code is as follows:
ASPX code
<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. OleDb" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Script runat = "server">
Int PageIndex = 1;
Private String ConnectionString = @ "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = | DataDirectory | MengXianHui. mdb ;";
Public int TotalCount = 0;
Public int PageItem = 5;
System. Data. DataView createperformancebyxianhuimeng ()
{
Int32.TryParse (Request. QueryString ["Page"], out PageIndex );
OleDbCommand cmd;
String SQL;
OleDbConnection cn = new OleDbConnection (ConnectionString );
Cn. Open ();
// Because it is an Access database, we only perform simple paging. For scenarios with high performance requirements, please use other methods, such as stored procedures.
SQL = "SELECT COUNT (*) FROM [Document]";
Cmd = new OleDbCommand (SQL, cn );
// Total number of records
TotalCount = Convert. ToInt32 (cmd. ExecuteScalar ());
// The serial number of the current page
If (PageIndex <1) PageIndex = 1;
Int PageCount = (int) Math. Ceiling (double) (TotalCount)/PageItem );
If (PageIndex> PageCount) PageIndex = PageCount;
Int startRecord = (PageIndex-1) * PageItem;
SQL = "SELECT distinct entid, DocumentGuid, Title, CreateDate FROM [Document] ORDER BY [distinct entid] DESC ";
OleDbDataAdapter da = new OleDbDataAdapter (SQL, cn );
DataSet ds = new DataSet ();
Da. Fill (ds, startRecord, PageItem, "Document ");
Cn. Close ();
Return ds. Tables [0]. DefaultView;
}
Protected void Page_Load (object sender, EventArgs e)
{
If (String. IsNullOrEmpty (Request. QueryString ["Page"])
{
// Load the display page content for the first time and initialize parameters.
Createperformancebyxianhuimeng ();
Page. ClientScript. RegisterStartupScript (Page. GetType (), "js", "Pager (1)", true );
}
Else
{
Response. ClearContent ();
GridView1.DataSource = createperformancebyxianhuimeng ();
GridView1.DataBind ();
System. Text. StringBuilder sb = new System. Text. StringBuilder ();
System. IO. StringWriter sw = new System. IO. StringWriter (sb );
HtmlTextWriter htw = new HtmlTextWriter (sw );
GridView1.RenderControl (htw );
Response. Write (sb. ToString ());
Response. End ();
}
}
/// Add this override void VerifyRenderingInServerForm (Control control) to avoid
/// The control "GridView1" of the type "GridView" must be placed in the form tag with runat = server.
/// Exception
Public override void VerifyRenderingInServerForm (Control control Control)
{}
</Script>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> simple AJAX partial refresh paging example </title>
<Script type = "text/javascript">
Function GetData (p ){
Var h = window. XMLHttpRequest? New window. XMLHttpRequest (): new ActiveXObject ("MSXML2.XMLHTTP ");
H. open ("GET", <% = Request. FilePath %>? Page = + p + "&" + Date. parse (new Date (), true );
H. onreadystatechange = function (){
If (h. readyState = 4 ){
If (h. status = 200 ){
Document. getElementById ("_ Containter"). innerHTML = h. responseText;
}
}
}
H. send (null );
}
///
/// Functions with paging function.
///
Function Pager (CurrentPage ){
GetData (CurrentPage );
Var TotalRows = <% = TotalCount %>;
Var Step = 3;
Var PageItem = <% = PageItem %>;
If (TotalRows <1 ){
TotalPage = 0;
}
Else {
TotalPage = Math. ceil (TotalRows/PageItem)
}
Var PagerContent = "Total" + TotalPage + "page per page" + PageItem + "items ";
Var leftStep = CurrentPage-Step;
Var rightStep = CurrentPage + Step;
If (leftStep <1) leftStep = 1;
If (rightStep> TotalPage) rightStep = TotalPage;
If (CurrentPage> 1) PagerContent + = "<a href = # onclick = Pager (1); return false;> homepage </a> <a href = onclick = Pager ("+ (CurrentPage-1) +"); return false;> previous page </a>"
For (var I = leftStep; I <= rightStep; I ++ ){
If (I = CurrentPage ){
PagerContent + = "<strong style = color: red>" + I + "</strong>"
}
Else {
PagerContent + = "<a href = # onclick = Pager (" + I + "); return false;>" + I + "</a>"
}
}
If (rightStep <TotalPage) PagerContent + = "<a href = onclick = Pager (" + (CurrentPage + 1) + "); return false;> next page </a> <a href = onclick = Pager ("+ TotalPage +"); return false;> last page </a>"
Document. getElementById ("_ Pager1"). innerHTML = document. getElementById ("_ Pager2"). innerHTML = PagerContent;
}
Alert ("the marker that appears when the page is loaded for the first time. ")
</Script>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div id = "_ Pager1" style = "padding: 10px; text-align: center"> </div>
<Div id = "_ Containter">
<Asp: GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "false" Width = "80%">
<HeaderStyle BackColor = "# EEEEEE"/>
<Columns>
<Asp: BoundField DataField = "entientid"/>
<Asp: HyperLinkField HeaderText = "article Title" DataNavigateUrlFields = "DocumentGuid" DataTextField = "Title"
DataNavigateUrlFormatString = "http://dotnet.aspx.cc/article/4100#/read.aspx"/>
<Asp: BoundField HeaderText = "Release Date" DataField = "CreateDate"/>
</Columns>