Download aspnetpager Control Address: http://www.webdiyer.com/AspNetPager/default.aspx
Reference it in the toolbox, and drag it directly to the location where you need to pagination.
Aspx FileCode:
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "aspnetpager. aspx. cs" inherits = "aspnetpager" %>
<% @ Register Assembly = "aspnetpager" namespace = "Wuqi. webdiyer" tagprefix = "webdiyer" %>
<! 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> use the aspnetpager control to implement gridview pagination </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: gridview id = "gridview1" runat = "server" style = "width: 100%">
</ASP: gridview>
<Webdiyer: aspnetpager id = "aspnetpager1" runat = "server" custominfohtml = "% pagecount % page in total, currently % currentpageindex % PAGE, % pagesize % per page"
Firstpagetext = "Homepage" lastpagetext = "last page" nextpagetext = "next page" prevpagetext = "Previous Page"
Showboxthreshold = "1" showcustominfosection = "Left" width = "100%" onpagechanging = "aspnetpager1_pagechanging">
</Webdiyer: aspnetpager>
</Div>
</Form>
</Body>
</Html>
CS file code:
Using system;
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;
Using system. Data. sqlclient;
Public partial class aspnetpager: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Binddata ();
}
}
Protected void aspnetpager1_pagechanging (Object SRC, Wuqi. webdiyer. pagechangingeventargs E)
{
Aspnetpager1.currentpageindex = E. newpageindex;
Binddata ();
}
# Region Binding data
Protected void binddata ()
{
String connstr = "Server =.; database = ***; uid = ***; Pwd = ***"; // set the database connection string
Sqlconnection conn = new sqlconnection (connstr); // connect to the database
Conn. open (); // open the database
String SQL = "select * from table name ";
Sqlcommand comm = new sqlcommand (SQL, Conn );
Sqldataadapter da = new sqldataadapter (Comm );
Dataset DS = new dataset ();
Da. Fill (DS, "table name ");
Pageddatasource PPS = new pageddatasource ();
PPS. datasource = Ds. Tables [0]. defaultview; // you can specify a data source for pagination.
PPS. allowpaging = true; // you can specify whether pagination is allowed.
Aspnetpager1.recordcount = Pam. Count; // aspnetpager1.recordcount = Ds. Tables [0]. defaultview. Count; equivalent // number of data records obtained
PPS. currentpageindex = aspnetpager1.currentpageindex-1; // you can specify the index of the current page.
PPS. pagesize = aspnetpager1.pagesize; // you can specify the number of pages displayed on each page.
This. gridview1.datasource = PDS;
This. gridview1.databind (); // bind data
}
# Endregion
}
This is the first time I used the paging control to read a piece of code written by someone else and write a piece of code myself.
During my test, I found that the total number of records displayed is only 10, and I have not found out why. Please help me to see why