Implementation of High-Efficiency pagination for massive (massive) data displayed in the gridview

Source: Internet
Author: User
How to implement high-efficiency Paging for massive (massive) data display in gridview

Source Network

Problem:

The gridview shows that the aging rate of massive data is extremely low. It is obviously unrealistic to retrieve massive data from the database each time.

Solution:

When the value is displayed, the data read from the database is obtained, that is, the page data displayed in the gridview is retrieved each time. Click the next page to retrieve the corresponding data on the next page.

It is really easy, but it is still a little difficult for people not familiar with Asp.net.

For example, when a page is refreshed, how does one keep the value of a variable unchanged? Dizzy, right?

Next we will discuss the analysisImplementation Method:

1. Refresh the page to keep the variable value unchanged

First, let's talk about how to keep a variable value unchanged when refreshing the page. Previously, I used the page-based value transfer method, which seems a bit strange. Originally, the data on this page must be stored through page-based value transfer.

Later, I found the vewstate on the Internet, which is a bit like session and basically the same in usage.

The difference between them is that sessions are between pages, while viewstate maintains a value on its own page.

Usage:

Viewstate ["pagecounts"] = 3;

The next time you want to use it, it will be OK.

 

2. Code for efficient paging of massive data in gridview

 

The following code is from the Internet:

 

Const int pagesize = 20; // defines the display records per page
Int pagecount, reccount, currentpage, pages, and jumppage; // defines several parameters for saving paging parameters.

Private void page_load (Object sender, system. eventargs E)
{
If (! Ispostback)
{
Reccount = calc (); // obtain the total number of records through the calc () function
Pagecount = reccount/pagesize + overpage ();

// Calculate the total number of pages (plus the overpage () function to prevent incomplete display data due to the remainder)

Viewstate ["pagecounts"] = reccount/pagesize-modpage ();

// Save the total page parameters to viewstate (minus the modpage () function to prevent overflow of the query range during SQL statement execution. 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. When you skip the page, determine whether the number of user inputs exceeds the page number range.
// Display the status of lpagecount and lrecordcount
Lpagecount. Text = pagecount. tostring ();
Lrecordcount. Text = reccount. tostring ();
// Determines whether the text box on the page is invalid.
If (reccount <= 20)
Gotopage. Enabled = false;
Tdatabind (); // call the data binding function tdatabind () for data binding
}
}
// Calculate the remainder page
Public int overpage ()
{
Int pages = 0;
If (reccount % pagesize! = 0)
Pages = 1;
Else
Pages = 0;
Return pages;
}
// Calculate the remaining page to prevent overflow of the query range during SQL statement execution
Public int modpage ()
{
Int pages = 0;
If (reccount % pagesize = 0 & reccount! = 0)
Pages = 1;
Else
Pages = 0;
Return pages;
}
/*
* Calculate the static function of the total record
* The reason for using static functions here is: If static data or functions are referenced, the connector will optimize the code generation and remove the dynamic relocation item (

The paging Effect of massive data tables is more obvious ).
* I hope you will give your comments and correct them if they are incorrect.
*/
Public static int calc ()
{
Int recordcount = 0;
Sqlcommand mycmd = new sqlcommand ("select count (*) as Co from redheadedfile", mycon ());
Sqldatareader DR = mycmd. executereader ();
If (dr. Read ())
Recordcount = int32.parse (Dr ["CO"]. tostring ());
Mycmd. Connection. Close ();
Return recordcount;
}
// Database connection Statement (obtained from web. config)
Public static sqlconnection mycon ()
{
Sqlconnection myconnection = new sqlconnection (configurationsettings. etettings ["DSN"]);
Myconnection. open ();
Return myconnection;
}
// Operate the commandname value returned by the four buttons (Home Page, Previous Page, next page, and last page)
Private void page_onclick (Object sender, commandeventargs E)
{
Currentpage = (INT) viewstate ["pageindex"];

// Read the page number value from viewstate and save it to the currentpage variable for Parameter Calculation
Pages = (INT) viewstate ["pagecounts"]; // calculates the total page parameter read from viewstate.

String cmd = E. commandname;
Switch (CMD) // filter commandname
{
Case "Next ":
Currentpage ++;
Break;
Case "Prev ":
Currentpage --;
Break;
Case "last ":
Currentpage = pages;
Break;
Default:
Currentpage = 0;
Break;
}
Viewstate ["pageindex"] = currentpage;

// Save the computed currentpage variable to viewstate again

Tdatabind (); // call the data binding function tdatabind ()
}

Private void tdatabind ()
{
Currentpage = (INT) viewstate ["pageindex"];

// Read the page number value from viewstate and save it to the currentpage variable for button Failure Calculation
Pages = (INT) viewstate ["pagecounts"];

// Read the total page parameters from viewstate for button invalidation Calculation
// Determine the status of the four buttons (Home Page, Previous Page, next page, and last page)
If (currentpage + 1> 1)
{
Fistpage. Enabled = true;
Prevpage. Enabled = true;
}
Else
{
Fistpage. Enabled = false;
Prevpage. Enabled = false;
}
If (currentpage = pages)
{
Nextpage. Enabled = false;
Lastpage. Enabled = false;
}
Else
{
Nextpage. Enabled = true;
Lastpage. Enabled = true;
}
// Bind data to the datalist Control
Dataset DS = new dataset ();
// The core SQL statement for query operations (which determines the paging efficiency :)
Sqldataadapter myadapter = new sqldataadapter ("select top" + pagesize + "* From redheadedfile where ID

Not in (select top "+ pagesize * currentpage +" id from redheadedfile order by id asc) order by id asc ", mycon ());
Myadapter. Fill (DS, "news ");
Datalist1.datasource = Ds. Tables ["news"]. defaultview;
Datalist1.databind ();
// Display the status of label controls lcurrentpaget and text box controls gotopage
Lcurrentpage. Text = (currentpage + 1). tostring ();
Gotopage. Text = (currentpage + 1). tostring ();
// Release the sqldataadapter
Myadapter. Dispose ();
}

// Page jumping code
Private void gotopage_textchanged (Object sender, system. eventargs E)
{
Try
{
Jumppage = (INT) viewstate ["jumppages"];

// Read the Available page number value from viewstate and save it to the jumppage variable

// Determine whether the user input value exceeds the Available page number range
If (int32.parse (gotopage. Text)> jumppage | int32.parse (gotopage. Text) <= 0)

Response. Write ("<SCRIPT> alert ('page number range exceeded! '); Location. href = 'webform8. aspx' </SCRIPT> ");
Else
{
Int inputpage = int32.parse (gotopage. Text. tostring ()-1;

// Convert the user input value and save it in the INT-type inputpage variable
Viewstate ["pageindex"] = inputpage;

// Write the inputpage value to viewstate ["pageindex "]
Tdatabind ();

// Call the data binding function tdatabind () to re-bind the data.
}
}
// Capture exceptions caused by incorrect data types entered by users
Catch (exception exp)
{
Response. Write ("<SCRIPT> alert ('" + exp. Message + "'); location. href = 'webform8. aspx '</SCRIPT> ");
}
}

 

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.