The DataGrid provides the paging function, but it seems to have limited functions. However, we can use some properties of the DataGrid to obtain the status and add buttons for the home page and the last page. The custom page function of the DataGrid is not used here. If the speed is not very efficient, it is good to manage the page by the DataGrid itself, the cost is to extract the entire data and then delete the data on the specified page. The advantage is that the development speed is fast and there is no need to write paging stored procedures. This example uses the northwind database in SQL Server. The running interface is as follows:
For the front-end display interface, I put a DataGrid, four linkbutton directing buttons, and four literal to display the record status.
The rest is positioning using tables.
Set the allowpaging attribute of the DataGrid to true, set the allowcustompaging attribute bit to false (default value: false), and set the visible attribute of pagerstyle to false so that the foreground is not displayed.
HTML:
Bytes -------------------------------------------------------------------------------------------------------------------------
<% @ Page Language = "C #" codebehind = "datagridpaging. aspx. cs" autoeventwireup = "false" inherits = "ZZ. aspnetpaging. datagridpaging" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> datagridpaging </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "generator">
<Meta content = "C #" name = "code_language">
<Meta content = "JavaScript" name = "vs_defaultclientscript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetschema">
</Head>
<Body>
<Form ID = "form1" method = "Post" runat = "server">
<Table id = "Table1" style = "font-size: 9pt" cellspacing = "1" cellpadding = "1" width = "450" align = "center"
Border = "1">
<Tr>
<TD>
<Asp: DataGrid id = "datagrid1" runat = "server" allowpaging = "true" width = "100%" pagesize = "5">
<Headerstyle font-size = "9pt">
<Footerstyle font-size = "9pt"> </footerstyle>
<Pagerstyle visible = "false" font-size = "9pt" mode = "numericpages"> </pagerstyle>
</ASP: DataGrid> </TD>
</Tr>
</Table>
<Table id = "Table2" style = "font-size: 9pt" cellspacing = "1" cellpadding = "1" width = "450" align = "center"
Border = "1">
<Tr>
<TD style = "width: 207px">
<Asp: linkbutton id = "lbtnfirst" runat = "server" commandname = "first"> homepage </ASP: linkbutton>
<Asp: linkbutton id = "lbtnprev" runat = "server" commandname = "Prev"> previous page </ASP: linkbutton>
<Asp: linkbutton id = "lbtnnext" runat = "server" commandname = "Next"> next page </ASP: linkbutton>
<Asp: linkbutton id = "lbtnlast" runat = "server" commandname = "last"> last page </ASP: linkbutton>
</TD>
<TD>
<Asp: literal id = "ltlpageindex" runat = "server"> </ASP: literal> page
<Asp: literal id = "ltlpagecount" runat = "server"> </ASP: literal> page
<Asp: literal id = "ltlpagesize" runat = "server"> </ASP: literal> total
<Asp: literal id = "ltlrecordcount" runat = "server"> </ASP: literal> entries
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
. CS code:
Certificate ----------------------------------------------------------------------------------------------------------------------------------------
Using system;
Using system. collections;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. Web;
Using system. Web. sessionstate;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Using system. configuration;
Namespace ZZ. aspnetpaging
{
Public class datagridpaging: system. Web. UI. Page
{
Private Static string connstring = configurationsettings. deleettings ["connstring"];
Private int recordcount;
Private int pagecount;
Protected system. Web. UI. webcontrols. linkbutton lbtnfirst;
Protected system. Web. UI. webcontrols. linkbutton lbtnprev;
Protected system. Web. UI. webcontrols. linkbutton lbtnnext;
Protected system. Web. UI. webcontrols. linkbutton lbtnlast;
Protected system. Web. UI. webcontrols. Literal ltlpageindex;
Protected system. Web. UI. webcontrols. Literal ltlpagecount;
Protected system. Web. UI. webcontrols. Literal ltlpagesize;
Protected system. Web. UI. webcontrols. Literal ltlrecordcount;
Protected system. Web. UI. webcontrols. DataGrid datagrid1;
Private void page_load (Object sender, system. eventargs E)
{
If (! Page. ispostback)
{
Datagriddatabind ();
}
}
// Bind data
Private void datagriddatabind ()
{
Dataset DS = getcustomersdata ();
Recordcount = Ds. Tables [0]. Rows. count;
// Obtain the current page number
Pagecount = (INT) math. Ceiling (recordcount * 1.0/pagesize );
// Avoid currentpageindex> pagecount errors when the record has been record-free and has been reversed
If (recordcount = 0)
{
This. Maid = 0;
}
Else if (this. datagrid1.currentpageindex> = pagecount)
{
This. datagrid1.currentpageindex = pagecount-1;
}
This. datagrid1.datasource = Ds;
This. datagrid1.databind ();
Navigationstatechange ();
}
# Code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. lbtnfirst. Click + = new system. eventhandler (this. lbtnnavigation_click );
This. lbtnprev. Click + = new system. eventhandler (this. lbtnnavigation_click );
This. lbtnnext. Click + = new system. eventhandler (this. lbtnnavigation_click );
This. lbtnlast. Click + = new system. eventhandler (this. lbtnnavigation_click );
This. Load + = new system. eventhandler (this. page_load );
}
# Endregion
Private void lbtnnavigation_click (Object sender, system. eventargs E)
{
Linkbutton BTN = (linkbutton) sender;
Switch (BTN. commandname)
{
Case "first ":
Pageindex = 0;
Break;
Case "Prev": // If (pageindex> 0)
Pageindex = pageindex-1;
Break;
Case "Next": // If (pageindex <pagecount-1)
Pageindex = pageindex + 1;
Break;
Case "last ":
Pageindex = pagecount-1;
Break;
}
Datagriddatabind ();
}
// Data Binding
Public static dataset getcustomersdata ()
{
Sqlconnection conn = new sqlconnection ("Server =.; database = northwind; uid = sa ");
String sqlstr = "select customerid, companyName, address, phone from customers ";
Sqlcommand comm = new sqlcommand (sqlstr, Conn );
Sqldataadapter dataadapter = new sqldataadapter (Comm );
Dataset DS = new dataset ();
Dataadapter. Fill (DS );
Return Ds;
}
/// <Summary>
/// Control the status of navigation buttons or numbers
/// </Summary>
Public void navigationstatechange ()
{
If (pagecount <= 1) // (recordcount <= pagesize) // a page smaller than or equal
{
This. lbtnfirst. Enabled = false;
This. lbtnprev. Enabled = false;
This. lbtnnext. Enabled = false;
This. lbtnlast. Enabled = false;
}
Else // multiple pages
{
If (pageindex = 0) // the current page is the first page
{
This. lbtnfirst. Enabled = false;
This. lbtnprev. Enabled = false;
This. lbtnnext. Enabled = true;
This. lbtnlast. Enabled = true;
}
Else if (pageindex = pagecount-1) // The last page
{
This. lbtnfirst. Enabled = true;
This. lbtnprev. Enabled = true;
This. lbtnnext. Enabled = false;
This. lbtnlast. Enabled = false;
}
Else // intermediate page
{
This. lbtnfirst. Enabled = true;
This. lbtnprev. Enabled = true;
This. lbtnnext. Enabled = true;
This. lbtnlast. Enabled = true;
}
}
If (recordcount = 0) // when no record exists, the DataGrid. pagecount page displays 1
This. ltlpagecount. Text = "0 ";
Else
This. ltlpagecount. Text = pagecount. tostring ();
If (recordcount = 0)
This. ltlpageindex. Text = "0 ";
Else
This. ltlpageindex. Text = (pageindex + 1). tostring (); // when there are pages, the front-end displays the number of pages plus 1
This. ltlpagesize. Text = pagesize. tostring ();
This. ltlrecordcount. Text = recordcount. tostring ();
}
// Total number of pages
Public int pagecount
{
Get {return this. datagrid1.pagecount ;}
}
// Page size
Public int pagesize
{
Get {return this. datagrid1.pagesize ;}
}
// Page index, starting from scratch
Public int pageindex
{
Get {return this. datagrid1.currentpageindex ;}
Set {This. datagrid1.currentpageindex = value ;}
}
// Total record count
Public int recordcount
{
Get {return recordcount ;}
Set {recordcount = value ;}
}
}
}