asp.net in the DataGrid custom paging (simple, practical, understandable) __.net

Source: Internet
Author: User
In ASP.net, the DataGrid custom paging is a common problem. In the project, many people use the Aspnetpage control to combine the efficient SQL stored procedure paging. This article will implement the paging requirements in the project in a simple, understandable way. The data table record in the project is 80000, 0. The paging speed is very fast. I control, in such a record, the use of the DataGrid with the paging function, in the switch to the next page, the CPU has the process of using 100%, while using this method to customize the paging, CPU use only a few percent oh ... using System ;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.SqlClient;
Using System.Configuration;
Namespace WS. Webs
{
/**////<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList DataList1;
protected System.Web.UI.WebControls.Label Lpagecount;
protected System.Web.UI.WebControls.Label Lrecordcount;
protected System.Web.UI.WebControls.LinkButton fistpage;
protected System.Web.UI.WebControls.LinkButton prevpage;
protected System.Web.UI.WebControls.LinkButton Nextpage;
protected System.Web.UI.WebControls.LinkButton lastpage;
protected System.Web.UI.WebControls.Label lcurrentpage;
protected System.Web.UI.WebControls.TextBox gotoPage;
Custom page-Numbering records
const int PAGESIZE=20;
int pagecount,reccount,currentpage,pages,jumppage;//defines several save paging parameter variables


private void Page_Load (object sender, System.EventArgs e)
{
if (! Page.IsPostBack)
{
RecCount = Calc ();//Get total number of records through the Calc () function
PageCount = reccount/pagesize + overpage ()//Total number of pages calculated (plus the overpage () function prevents the display data from being incomplete due to an allowance)
viewstate["pagecounts"] = Reccount/pagesize-modpage ()//Save the total page parameter to ViewState (minus the modpage () function to prevent overflow of the query scope when the SQL statement is executed. You can use the stored procedure paging algorithm to understand this sentence.
viewstate["PageIndex"] = 0;//Save a page index value of 0 to ViewState
viewstate["jumppages"] = pagecount;//save PageCount to ViewState to determine if user input exceeds page range when skipping page
Shows the status of Lpagecount, Lrecordcount
Lpagecount.text = Pagecount.tostring ();
Lrecordcount.text = Reccount.tostring ();
To determine if a page-skipping text box fails
if (RecCount <= 20)
gotopage.enabled = false;
Tdatabind ()//Call data-binding function Tdatabind () for data-binding operations

}
}

Calculate remaining pages
public int Overpage ()
{
int pages = 0;
if (reccount%pagesize!= 0)
pages = 1;
&nb
Related Article

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.