ASP. NET high-performance Paging

Source: Internet
Author: User
The first is the stored procedure. Only the data that I need is retrieved. If the number of pages exceeds the total number of data records, the records on the last page are automatically returned: Program Code Set ansi_nulls on
Set quoted_identifier on
Go
-- ===================================================== ======
-- Author: Clear
-- Create Date: 2007-01-30
-- Description: High-Performance Paging
-- ===================================================== ======
Alter procedure [DBO]. [tag_page_name_select]
-- Input the maximum number of records and the current page number.
@ Maxpagesize int,
@ Pagenum int,
-- Set an output parameter to return the total number of records for the paging list
@ Count int output
As
Begin
Set nocount on;

Declare
-- Define sorting name Parameters
@ Name nvarchar (50 ),
-- Define the cursor position
@ Cursor int
-- Obtain the total number of records first
Select @ COUNT = count (tag_name)
From [viewdatabase0716]. [DBO]. [view_tag];
-- Define the position where the cursor needs to begin
Set @ cursor = @ maxpagesize * (@ PageNum-1) + 1
-- If the cursor is greater than the total number of records, place the cursor at the beginning of the last page.
If @ cursor> @ count
Begin
-- If the last page is equal to the maximum number of records each time, the last page is returned.
If @ count % @ maxpagesize = 0
Begin
If @ cursor> @ maxpagesize
Set @ cursor = @ count-@ maxpagesize + 1
Else
Set @ cursor = 1
End
-- Otherwise, the remaining records on the last page will be returned.
Else
Set @ cursor = @ count-(@ count % @ maxpagesize) + 1
End
-- Point the pointer to the beginning of the page
Set rowcount @ cursor
-- Get the start position of the record
Select @ name = tag_name
From [viewdatabase0716]. [DBO]. [view_tag]
Order by tag_name;
-- Set the start position
Set rowcount @ maxpagesize
-- Get this page record
Select *
From [viewdatabase0716]. [DBO]. [view_tag]
Where tag_name >=@ name
Order by tag_name

Set rowcount 0
End

Then there is the paging control (... For the omitted HTML code generation method ):

Program code 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. text;

/// <Summary>
/// Extended connection string
/// </Summary>
Public class exstringbuilder
{
Private stringbuilder insertstring;
Private stringbuilder pagestring;
Private int privatepagenum = 1;
Private int privatemaxpagesize = 25;
Private int privatemaxpages = 10;
Private int privatecount;
Private int privateallpage;
Public exstringbuilder ()
{
Insertstring = new stringbuilder ("");
}
/// <Summary>
/// Obtain the generated HTML
/// </Summary>
Public String gethtml
{
Get
{
Return insertstring. tostring ();
}
}
/// <Summary>
/// Obtain the generated page html
/// </Summary>
Public String getpagehtml
{
Get
{
Return pagestring. tostring ();
}
}
/// <Summary>
/// Set or obtain the current page number
/// </Summary>
Public int pagenum
{
Get
{
Return privatepagenum;
}
Set
{
If (value> = 1)
{
Privatepagenum = value;
}
}
}
/// <Summary>
/// Set or obtain the maximum number of minutes
/// </Summary>
Public int maxpagesize
{
Get
{
Return privatemaxpagesize;
}
Set
{
If (value> = 1)
{
Privatemaxpagesize = value;
}
}
}
/// <Summary>
/// Set or obtain the maximum number of pages displayed each time
/// </Summary>
Public int maxpages
{
Get
{
Return privatemaxpages;
}
Set
{
Privatemaxpages = value;
}
}
/// <Summary>
/// Set or retrieve the total number of data items
/// </Summary>
Public int datecount
{
Get
{
Return privatecount;
}
Set
{
Privatecount = value;
}
}
/// <Summary>
/// Obtain the total number of pages of data
/// </Summary>
Public int allpage
{
Get
{
Return privateallpage;
}
}
/// <Summary>
/// Initialize the page
/// </Summary>
Public void pagination ()
{
Pagestring = new stringbuilder ("");
// Obtain the total number of pages
Privateallpage = (INT) math. Ceiling (decimal) privatecount/(decimal) privatemaxpagesize );
// Prevent the superscript or subscript from crossing the border
If (privatepagenum> privateallpage)
{
Privatepagenum = privateallpage;
}
// Scroll cursor paging Mode
Int leftrange, rightrange, leftstart, rightend;
Leftrange = (privatemaxpages + 1)/2-1;
Rightrange = (privatemaxpages + 1)/2;
If (privatemaxpages> = privateallpage)
{
Leftstart = 1;
Rightend = privateallpage;
}
Else
{
If (privatepagenum <= leftrange)
{
Leftstart = 1;
Rightend = leftstart + privatemaxpages-1;
}
Else if (privateallpage-privatepagenum <rightrange)
{
Rightend = privateallpage;
Leftstart = rightend-privatemaxpages + 1;
}
Else
{
Leftstart = privatepagenum-leftrange;
Rightend = privatepagenum + rightrange;
}
}

// Generate page number list statistics
Pagestring. append (...);

Stringbuilder previusstring = new stringbuilder ("");
// If on the first page
If (privatepagenum> 1)
{
...
}
Else
{
...
}
// If the first page is displayed
If (privatepagenum> privatemaxpages)
{
...
}
Else
{
...
}
Pagestring. append (previousstring );
// Generate the intermediate page
For (INT I = leftstart; I <= rightend; I ++)
{
// When the current page
If (I = privatepagenum)
{
...
}
Else
{
...
}
}
Stringbuilder laststring = new stringbuilder ("");
// If the last page
If (privatepagenum <privateallpage)
{
...
}
Else
{
...
}
// If the last group
If (privatepagenum + privatemaxpages) <privateallpage)
{
...
}
Else
{
...
}
Pagestring. append (laststring );
}
/// <Summary>
/// Generate a tag classification table
/// </Summary>
Public void tagtable (exdatarow myexdatarow)
{
Insertstring. append (...);
}

Call method:

Program code // get the paging settings and put it into the session
Exrequest myexrequest = new exrequest ();
Myexrequest. pagesession ("tag _", new string [] {"page", "size "});
// Generate tag pagination
Exstringbuilder tag = new exstringbuilder ();
// Set how many records are displayed each time
Tag. maxpagesize = convert. toint32 (session ["tag_size"]);
// Set the maximum number of pages displayed
Tag. maxpages = 9;
// Set the current page
Tag. pagenum = convert. toint32 (session ["tag_page"]);
String [] [] mynamenvalue = new string [2] [] {
New String [] {"maxpagesize", "pagenum", "Count "},
New String [] {tag. maxpagesize. tostring (), tag. pagenum. tostring ()}
};
// Call the Stored Procedure
Datatable mydatatable = mysql. batchgetdb ("tag_page_name_select", mynamenvalue, "Count ");
Tag. datecount = (INT) mysql. outputcommand. Parameters ["@ count"]. value;
Tag. pagination ();

Headpage. innerhtml = footpage. innerhtml = tag. getpagehtml;

For (INT I = 0, j = mydatatable. Rows. Count; I <j; I ++)
{
Tag. tagtable (New exdatarow (mydatatable. Rows [I]);
}
Tagbox. innerhtml = tag. gethtml;

The method for handling the page number to the session is not provided. the method for calling the parameters and records returned by the stored procedure is similar to the method for batch data operation I previously wrote. You only need to define an output method.

At present, I think this code will still be defective. Let's wait for the code review in the project to be strengthened later. What I want to say is not to be confused by the drag-and-drop items, in this way, we will never improve ourselves. We need to do one thing with an attitude of knowing its own, so that our help will be obvious.

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.