Use the aspnetpager control to implement the gridview pagination

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.