Asp. NET Data List Control page summary (iii) asp.net customizable paging user control

Source: Internet
Author: User

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 (); }
}

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.