ASP. NET high-performance paging code

Source: Internet
Author: User

Recently, pages are getting killed. I remember that I used to modify dw asp pages. Later I wrote a manual ASP page. Now I enter. NET, of course, should work with the stored procedure to create a pure manual High-Performance paging.

Why is it high performance? Why do we need to create it manually without using the. NET existing paging control? This will also be traced back to my modifying dw asp pages. I still don't know much about the program. I only need to repair and supplement it, so I don't want to talk about any performance issues. at that time, I was very upset and asked my private technical director, Mr. Zhang, to help me see it. At that time, Mr. Zhang gave me a disdainful look and said, "Is it worth it?

Then I created an ASP page by hand, and I couldn't solve it any more. I lost a bunch of pages to me. NET code: study it by yourself. then I lost another sentence: Use. NET.

Later, I found that all the previous pages were read from the entire dataset and then processed by page. Once the data volume is too large, the processing will be very slow, and even the server will crash. then, the previous pages cannot scroll like a cursor and are always fixed in a group. It is impossible to achieve the effect of the current page number in the middle.

Next we will talk about it. NET page control, but the defect is the first problem I found. It is inefficient to read and process all the data, so I finally started to create ASP by hand. NET high-performance paging.

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:Copy codeThe Code is as follows: 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
Set @ Cursor = @ Count-@ MaxPageSize + 1
-- 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 ):
Copy codeThe Code is as follows: 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:
Copy codeThe Code is as follows: // 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.