The previous article is to use stored procedures for paging, but there are defects, no code reuse, for different tables or different views need to write stored procedures, so cumbersome, code reusability is poor. This article will use a user control to implement the page layer of data paging to achieve the purpose of code reuse.
Pagination is implemented with the help of user-defined controls, where there are two main ways to accomplish this:
(i). Wuqi Aspnetpager Components (recommended)
First, download the DLL file.
In the Toolbox, right-click, select Item, and add the DLL file that you just downloaded.
You will then see a Aspnetpager page control in the Toolbox, drag onto the page, and drag a data display control, GridView or repeater.
Finally, write the background code as follows:
Using System;
Using System.Data;
Using System.Configuration;
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 _default:system.web.ui.page {protected void Page_Load (object sender, EventArgs e) {if (!i
Spostback) {binddata (); }//Get and bind data public void Binddata () {//Connection database string Strcon = "server=.;
Database=northwind;uid=sa;pwd= ";
SqlConnection con = new SqlConnection (Strcon);
String sql = "SELECT * from Customers";
SqlDataAdapter SDA = new SqlDataAdapter (Sql,con);
DataSet ds = new DataSet (); Sda.
Fill (DS); Aspnetpager How the paging control uses DataView DV = ds. Tables[0].
DefaultView;
Use the PagedDataSource class PagedDataSource PDS = new PagedDataSource (); Aspnetpager1.recordcount = dv. count;//get total number of records PDS.
DataSource = DV; Pds.
AllowPaging = true; //Gets the index PDS for the current page.
CurrentPageIndex = aspnetpager1.currentpageindex-1; Pds. PageSize = aspnetpager1.pagesize;//Gets the number of records displayed per page Gridview1.datasource = PDS;
Specifies the data source Gridview1.databind ();
} protected void Aspnetpager1_pagechanged (object sender, EventArgs e) {binddata (); }
}