Self-write paging algorithm

Source: Internet
Author: User

I encapsulate it in the user control. You can also encapsulate it in class files. Custom:
[Csharp]
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Text;
 
Namespace HHWeb. UserControl
{
Public partial class Paging: System. Web. UI. UserControl
{
Public StringBuilder PagingControlInfo = new StringBuilder ();
Protected void Page_Load (object sender, EventArgs e)
{
GetPaging ();
}
Void GetPaging ()
{
// Determine if the current page is negative or zero, NowPage = 1
If (NowPage <= 0)
NowPage = 1;
// Determine if the current page is greater than the total number of pages, NowPage = PageCount
Int PageCount = GetPageCount ();
If (NowPage> PageCount)
NowPage = PageCount;
// Homepage and previous page ()
If (NowPage! = 1)
{
PagingControlInfo. Append ("<a href = \" "+ PageName + "? NowPage = 1 \ "> homepage </a> ");
PagingControlInfo. Append ("<a href = \" "+ PageName + "? NowPage = "+ (NowPage-1) +" \ "> previous page </a> ");
}
// If the total page number is smaller than the page number (PageNumber), The ellipsis (...) is not displayed (...)
If (PageCount <= PageNumber)
{
For (int I = 1; I <= PageCount; I ++)
GetPageNumberStr (I );
}
Else
{
If (PageNumber % 2! = 0)
{
LeftPageNumberOne = (PageNumber/2) + 1;
LeftPageNumberTwo = (PageNumber/2 );
RightPageNumber = (PageNumber/2 );
}
Else
{
LeftPageNumberOne = (PageNumber/2) + 1;
LeftPageNumberTwo = (PageNumber/2 );
RightPageNumber = (PageNumber/2)-1;
}
// When the total page number is greater than the page number (PageNumber) (simple processing-algorithm)
If (NowPage> LeftPageNumberOne & NowPage <PageCount-RightPageNumber)
{
PagingControlInfo. Append ("<span>... </span> ");
For (int I = NowPage-LeftPageNumberTwo; I <= NowPage + RightPageNumber; I ++)
GetPageNumberStr (I );
PagingControlInfo. Append ("<span>... </span> ");
}
Else if (NowPage> = PageCount-RightPageNumber)
{
PagingControlInfo. Append ("<span>... </span> ");
For (int I = (PageCount-PageNumber) + 1; I <= PageCount; I ++)
GetPageNumberStr (I );
}
Else
{
For (int I = 1; I <= PageNumber; I ++)
GetPageNumberStr (I );
PagingControlInfo. Append ("<span>... </span> ");
}
}
// Next and last pages
If (NowPage! = PageCount)
{
PagingControlInfo. Append ("<a href = \" "+ PageName + "? NowPage = "+ (NowPage + 1) +" \ "> next page </a> ");
PagingControlInfo. Append ("<a href = \" "+ PageName + "? NowPage = "+ PageCount +" \ "> last page </a> ");
}
}
 
# Region // page number splicing Method
Public void GetPageNumberStr (int I)
{
If (I = NowPage)
PagingControlInfo. Append ("<a href = \" "+ PageName + "? NowPage = "+ I +" \ "style = \" border: 0px; color: # ff5a00; \ ">" + I + "</a> ");
Else
PagingControlInfo. Append ("<a href = \" "+ PageName + "? NowPage = "+ I +" \ ">" + I + "</a> ");
}
# Endregion
 
# Region // calculate the total number of pages
Public int GetPageCount ()
{
If (PageSize = 0)
PageSize = 20; // if the number of records displayed on each page is "0", the default value is "20"
If (Totalcount % PageSize = 0)
Return (Totalcount/PageSize );
Else
Return (Totalcount/PageSize) + 1;
}
# Endregion
 
# Region // basic attributes required for paging
/// <Summary>
/// Current page
/// </Summary>
Public int NowPage {get; set ;}
/// <Summary>
/// Number of records displayed on each page
/// </Summary>
Public int PageSize {get; set ;}
/// <Summary>
/// Total number of entries
/// </Summary>
Public int Totalcount {get; set ;}
/// <Summary>
/// Page number (indicating the number of pages displayed by the front-end Page Control)
/// </Summary>
Public int PageNumber {get; set ;}
/// <Summary>
/// The name of the current page, for example, "index. aspx"
/// </Summary>
Public string PageName {get; set ;}
# Endregion
 
# Region // basic page attributes
/// <Summary>
/// Attribute 1 of the Left page number
/// </Summary>
Public int LeftPageNumberOne {get; set ;}
/// <Summary>
/// Attribute 2 of the Left page number
/// </Summary>
Public int LeftPageNumberTwo {get; set ;}
/// <Summary>
/// Right page number
/// </Summary>
Public int RightPageNumber {get; set ;}
# Endregion
}
}

The effect is as follows: Click the paging style and you will be OK. Www.2cto.com


Author: yfz19890410

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.