The use of the asp.net paging control AspNetPager, using the traditional paging and stored procedure Paging

Source: Internet
Author: User

Record the usage of the paging control. First, record the use of the AspNetPager paging control.

The AspNetPager paging controls are: http://www.webdiyer.com/Controls/AspNetPager/Downloads

Online help documentation: http://www.webdiyer.com/AspNetPagerDocs/index.html

Several important attributes of AspNetPager

CurrentPageIndex gets or sets the index of the currently displayed page.

PageSize gets or sets the number of items displayed on each page.

PageCount obtains the total number of pages required for all records to be paged.

CustomInfoHTML obtains or sets the custom HTML text displayed in the User-Defined information area.

Use FirstPageText to obtain or set it to the text displayed on the first page.

LastPageText obtains or sets the text displayed on the last page.

PrevPageText obtains or sets the text displayed on the previous page.

RecordCount gets or sets the total number of all records to be paged.

AlwaysShow gets or sets a value that specifies whether the AspNetPager page is always displayed, even if the data to be paged has only one page.

ShowPageIndex gets or sets a value indicating whether to display the page index value button in the page navigation element.

ShowPrevNext gets or sets a value indicating whether to display the previous and next pages in the page navigation element.

UrlPaging gets or sets whether to enable url to pass paging information.

 

1. aspnetpager uses the traditional paging Method

Use the Repeater control and the AspNetPager control to implement traditional paging,

 

Private void BindRepeater ()
{
String SQL = "select * from tb_Roles"; // custom SQL statement
Int recordcount;
SqlCommand cmd = new SqlCommand (SQL, GetConnection ());
Cmd. CommandType = CommandType. Text;
SqlDataAdapter ada = new SqlDataAdapter (cmd );
DataSet ds = new DataSet ();
Int startRow = (this. AspNetPager1.CurrentPageIndex-1) * this. AspNetPager1.PageSize;
Ada. Fill (ds, startRow, this. AspNetPager1.PageSize, "table ");
Recordcount = GetPageRecord (SQL );
This. AspNetPager1.RecordCount = recordcount;
This. Repeater1.DataSource = ds;
This. Repeater1.DataBind ();
}

In addition, it is bound to the AspNetPager page control PageChanged event.

Protected void AspNetPager1_PageChanged (object sender, EventArgs e)
{
This. BindRepeater ();
}

In addition, the method for getting the total number of records

Public int GetPageRecord (string SQL)
{
SQL = System. Text. RegularExpressions. Regex. Replace (SQL, "ORDER .*","");
SQL = "select count (*) from (" + SQL + ") as temp ";
SqlCommand cmd = new SqlCommand (SQL, GetConnection ());
Cmd. Connection. Open ();
Int recordcount = (int) cmd. ExecuteScalar ();
Cmd. Connection. Close ();
Return recordcount;
}

2. Use Stored Procedure pagination in aspnetpager

Call a stored procedure to obtain data

Private void BindListView ()
{
This. AspNetPager2.RecordCount = GetPageRecord ("select * from tb_Groups"); // obtain the total number of records
SqlCommand cmd = new SqlCommand ("pager", GetConnection ());
Cmd. CommandType = CommandType. StoredProcedure; // use the Stored Procedure
Int startRow = (this. AspNetPager2.CurrentPageIndex-1) * this. AspNetPager2.PageSize; // start point
Int endRow = this. AspNetPager2.CurrentPageIndex * this. AspNetPager2.PageSize;
SqlParameter [] parameters = new SqlParameter [] {
New SqlParameter ("@ startIndex", startRow ),
New SqlParameter ("@ endIndex", endRow ),
New SqlParameter ("@ docount", '0 '),
};
Cmd. Parameters. AddRange (parameters );
SqlDataAdapter ada = new SqlDataAdapter (cmd );
DataSet ds = new DataSet ();
Ada. Fill (ds, "table ");
This. GridView1.DataSource = ds;
This. GridView1.DataBind ();
}

You also need to call the binding method in the PageChanged event.

Finally, the stored procedure code is generated using the AspNetPager control. Enter the relevant information in the Wizard to generate the stored procedure code.

 

 

Click to open:

Based on the details, click Generate stored procedure and copy it to the clipboard. You can copy the script and run it in the database management system to generate the stored procedure.

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.