Asp.net (C #) high-efficiency paging algorithm for massive data tables

Source: Internet
Author: User

 

First, create a table (the ID is required to be automatically numbered ):
Create table redheadedfile (
Id int identity (1, 1 ),
Filenames nvarchar (20 ),
Senduser nvarchar (20 ),
Primary key (id)
)
Then we write 0.5 million records:
Declare @ I int
Set @ I = 1
While I <= 500000
Begin
Insert into redheadedfile (filenames, senduser) values (My paging algorithm, Lu junming)
Set @ I = @ I + 1
End
GO
Use Microsoft Visual Studio. NET 2003 to create a WebForm webpage (my name is webform8.aspx)
The front-end code snippet is as follows (webform8.aspx ):
<% @ Page language = "c #" Codebehind = "WebForm8.aspx. cs" AutoEventWireup = "false" Inherits = "WebApplication6.WebForm8" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> WebForm8 </title>
<Meta content = "Microsoft Visual Studio. NET 7.1" name = "GENERATOR">
<Meta content = "C #" name = "CODE_LANGUAGE">
<Meta content = "JavaScript" name = "vs_defaultClientScript">
<Meta content = "http://schemas.microsoft.com/intellisense/ie5" name = "vs_targetSchema">
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<Asp: datalist id = "datalist1" AlternatingItemStyle-BackColor = "# f3f3f3" Width = "100%" CellSpacing = "0"
CellPadding = "0" Runat = "server">
<ItemTemplate>
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
& Lt; td width = "30%"

Align = "center"> <% # DataBinder. Eval (Container. DataItem, "filenames") %> </td>
& Lt; td width = "30%"

Align = "center"> <% # DataBinder. Eval (Container. DataItem, "senduser") %> </td>
& Lt; td width = "30%"

Align = "center"> <% # DataBinder. Eval (Container. DataItem, "id") %> </td>
</Tr>
</Table>
</ItemTemplate>
</Asp: datalist>
<Div align = "center"> total <asp: label id = "LPageCount" Runat = "server" ForeColor = "# ff0000"> </asp: label> pages/Total

<Asp: label id = "LRecordCount" Runat = "server" ForeColor = "# ff0000"> </asp: label> record
<Asp: linkbutton id = "Fistpage" Runat = "server"

CommandName = "0"> homepage </asp: linkbutton> & nbsp; <asp: linkbutton id = "Prevpage" Runat = "server" CommandName = "prev">

Previous Page </asp: linkbutton> & nbsp; <asp: linkbutton id = "Nextpage" Runat = "server"

CommandName = "next"> next page </asp: linkbutton> & nbsp; <asp: linkbutton id = "Lastpage" Runat = "server"

CommandName = "last"> last page </asp: linkbutton> & nbsp; current <asp: label id = "LCurrentPage" Runat = "server"

ForeColor = "# ff0000"> </asp: label> page & nbsp; hop <asp: textBox ID = "gotoPage" Runat = "server" Width = "30px"

MaxLength = "5" AutoPostBack = "True"> </asp: TextBox> </div>
</Form>
</Body>
</HTML>
The background code snippet is as follows (webform8.aspx. cs)
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 WebApplication6
{
/// <Summary>
/// Summary of WebForm8.
/// </Summary>
Public class WebForm8: System. Web. UI. Page
{
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. DataList datalist1;
Protected System. Web. UI. WebControls. DropDownList mydroplist;
Protected System. Web. UI. WebControls. Label LPageCount;
Protected System. Web. UI. WebControls. Label LRecordCount;
Protected System. Web. UI. WebControls. Label LCurrentPage;
Protected System. Web. UI. WebControls. TextBox gotoPage;
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 display due to the remainder

Incomplete Data)

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 jumping off a page, determine whether the number of user inputs exceeds the page

Code 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"].

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.