asp.net Universal Paging display auxiliary class (improved version) _ Practical Tips

Source: Internet
Author: User
Tags dotnet prev
In your spare time to look back at many of the previous project code, found that there are always a bit of a flaw in the code, such as the barbed bone, annoying. Struggled for a long, well-meaning advised their own future to strive to write some code easier to use more flexible and more perfect some. For example, the common client Paging Display helper class (Aspnetpager), which is already mentioned, has a very high frequency in large and small projects, but obviously has a weaker control over the selection and style of pagination. Although the previous implementation of this function is based on the actual project requirements to achieve, the old project has no impact, but this is not the function of the full expansion and universality of the reasons, this is always proud of the building pig also have to admit.

1. Paging mode and style extension
(1) Defining enumerations
Copy Code code as follows:

<summary>
Table Alignment Enumeration
</summary>
public enum Tbalignenum
{
left = 1,

Center = 2,

right = 3
}

<summary>
Paging mode
</summary>
public enum Pagermodoule
{
<summary>
Normal paging mode
</summary>
Normal = 1,

<summary>
Statistical paging mode
</summary>
Statistics = 2
}

(2) rendering based on enumeration
Copy Code code as follows:

<summary>
Pagination Utility Class
</summary>
public sealed class Aspnetpager
{
#region Common

Private Const string defaultalign = "center";//default alignment
Private Const string leftalign = "Left";
Private Const string rightalign = "right";

<summary>
Get pages
</summary>
<param name= "OBJS" > Total record number </param>
<param name= "Recordcountperpage" > Number of records per page </param>
<returns></returns>
public static int getpagecout (object[] objs, int recordcountperpage)
{
return (int) math.ceiling ((double) objs. Length/(double) recordcountperpage));
}

<summary>
Get pages
</summary>
<param name= "TotalCount" > Total record number </param>
<param name= "Recordcountperpage" > Number of records per page </param>
<returns></returns>
public static int getpagecout (int totalcount, int recordcountperpage)
{
int result = 0;
if (totalcount% recordcountperpage = 0)
{
result = Totalcount/recordcountperpage;
}
Else
{
result = Totalcount/recordcountperpage + 1;
}
return result;
}

#endregion

#region Render Pager

<summary>
Write pagination page number (no table)
</summary>
<param name= "PagerMode" ></param>
<param name= "Response" ></param>
<param name= "Basestring" ></param>
<param name= "TotalCount" > Total record number </param>
<param name= "nowpage" > Current page </param>
<param name= "Recordcountperpage" > Number of records per page </param>
public static void Renderpager (Pagermodoule pagermode, httpresponse response, int totalcount, int nowpage, int recordCount Perpage, String basestring)
{
int pagecount = Getpagecout (TotalCount, recordcountperpage);
String pagerstring = String. Empty;
if (PageCount > 0)
{
Switch (PagerMode)
{
Case Pagermodoule.normal:
pagerstring = Createlinkurl (basestring, PageCount, Nowpage, recordcountperpage);
Break
Case Pagermodoule.statistics:
pagerstring = Createstatisticlinkurl (basestring, TotalCount, PageCount, Nowpage, recordcountperpage);
Break
Default
pagerstring = Createlinkurl (basestring, PageCount, Nowpage, recordcountperpage);
Break
}
Response. Write (pagerstring);
}
}

<summary>
Write pagination page number (with table)
</summary>
<param name= "PagerMode" ></param>
<param name= "Alignenum" ></param>
<param name= "Response" ></param>
<param name= "Basestring" ></param>
<param name= "TotalCount" > Total record number </param>
<param name= "nowpage" > Current page </param>
<param name= "Recordcountperpage" > Number of records per page </param>
public static void Rendertablepager (Pagermodoule pagermode, Tbalignenum alignenum, httpresponse response, int totalcount , int nowpage, int recordcountperpage, string basestring)
{
int pagecount = Getpagecout (TotalCount, recordcountperpage);
if (PageCount > 0)
{
String align = string. Empty;
Switch (alignenum)
{
Case Tbalignenum.left:
align = leftalign;
Break
Case Tbalignenum.center:
align = defaultalign;
Break
Case Tbalignenum.right:
align = rightalign;
Break
Default
align = defaultalign;
Break
}
StringBuilder sbtable = new StringBuilder ();
Sbtable.appendformat ("<table><tr align= ' {0} ' ><td>", align);
String pagerstring = String. Empty;
Switch (PagerMode)
{
Case Pagermodoule.normal:
pagerstring = Createlinkurl (basestring, PageCount, Nowpage, recordcountperpage);
Break
Case Pagermodoule.statistics:
pagerstring = Createstatisticlinkurl (basestring, TotalCount, PageCount, Nowpage, recordcountperpage);
Break
Default
pagerstring = Createlinkurl (basestring, PageCount, Nowpage, recordcountperpage);
Break
}
Sbtable.append (pagerstring);
Sbtable.append ("</td></tr></table>");
Response. Write (Sbtable.tostring ());
}
}

#endregion

#region Create link

<summary>
The raw page string (showing the number of pages associated with each page of records)
</summary>
<param name= "Basestring" ></param>
<param name= "PageCount" > Page </param>
<param name= "nowpage" > Current page </param>
<param name= "Recordcountperpage" > Number of records per page (Recommended records: Ten) </param>
<returns></returns>
private static string Createlinkurl (string basestring, int pagecount, int nowpage, int recordcountperpage)
{
StringBuilder sb = new StringBuilder ();
int from, to;
if (Nowpage-recordcountperpage > 0)
{
from = Nowpage-recordcountperpage;
}
Else
from = 1;
if (PageCount = 0)
PageCount = 1;
if (Pagecount-nowpage-recordcountperpage > 0)
{
to = Nowpage + recordcountperpage;
}
Else
to = PageCount;

if (Basestring.indexof ("?") = = 1)
Basestring + = "?";
Else
basestring = "&";
Sb. Append (String. Format ("<a href=\" {0}pageindex=1\ "> Home </a>", basestring));
if (PageCount > 1 && nowpage > 1)
{
Sb. AppendFormat ("<a href=\" {0}pageindex={1}\ "> Prev </a>", basestring, (nowPage-1). ToString ());
}
Else
{
Sb. Append ("<a href=" javascript:void (0); ' style= ' Color:gray; ' > Prev </a> ');
}
for (int i = from; I <= to; i++)
{
if (i = = nowpage)
{
Sb. AppendFormat ("<a href=" javascript:void (0); ' style= ' color:red; ' >{0}</a> ', nowpage.tostring ());
}
Else
{
Sb. AppendFormat ("<a href=\" {0}pageindex={1}\ ">{1}</a>", basestring, I);
}
}
if (PageCount > 1 && nowpage < PageCount)
{
Sb. AppendFormat ("<a href=\" {0}pageindex={1}\ "> next page </a>", basestring, (Nowpage + 1). ToString ());
}
Else
{
Sb. Append ("<a href=\" javascript:void (0); \ "Style= ' Color:gray; ' > Next page </a>");
}
Sb. Append (String. Format ("<a href={0}pageindex={1} > Last </a>", basestring, PageCount));
Return SB. ToString ();
}

<summary>
Generate a paging string with statistics (relative to the number of pages displayed and per page of records)
</summary>
<param name= "Basestring" ></param>
<param name= "TotalCount" > Total record number </param>
<param name= "PageCount" > Page </param>
<param name= "nowpage" > Current page </param>
<param name= "Recordcountperpage" > Number of records per page (Recommended records: Ten) </param>
<returns></returns>
private static string Createstatisticlinkurl (string basestring, int totalcount, int pagecount, int nowpage, int Recordcoun Tperpage)
{
StringBuilder sb = new StringBuilder ();
String Numricpager = Createlinkurl (basestring, PageCount, Nowpage, recordcountperpage);//Normal digital paging
Sb. AppendFormat ("A total of <span style= ' color:red;") >{0}</span> Records, a total of <span style= ' color:red; >{1}</span> page, current <span style= ' color:red; ' >{2}</span> page ",
TotalCount, PageCount, nowpage);
Sb. Append (Numricpager);
Return SB. ToString ();
}

#endregion
}

PS1: In addition to the table content alignment, the code control of the style is almost always hard code, the individual think the coding phase of the control of the style less the better. Ideally, the style should have all of the UI in the external control, rather than the programmer to design it, where the compromise solution is taken.
PS2: Pagination display can actually also expand, refer to the GridView control paging mode.
2. Call inside the page
Copy Code code as follows:

<%@ Page language= "C #" autoeventwireup= "true" codebehind= "Pager.aspx.cs" inherits= "Webtest.pager"%>

<%@ Import namespace= "DotNet.Common.WebForm"%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title>aspnet Simple Paging </title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<%dotnet.common.webform.aspnetpager.rendertablepager (Pagermodoule.normal, Tbalignenum.right, Response, 93, 10, "Pager.aspx"); %>
</div>
</form>
</body>

3, there is a picture of the truth
(1), Normal mode


(2), with statistical mode

Finally, welcome to add, look forward to your suggestions and comments.

Demo file Package download

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.