Asp.net generic paging display helper class

Source: Internet
Author: User
Tags dotnet
When using ASP. NET programming, if you do not need to use the default paging function of the control, you can look at the Asp.net generic paging display helper class in this article.

Review many previous projects in your spare timeCodeIt is annoying to find a lot of code that always has such a point of defect, such as a sting. Struggling for a long time, I am eager to recommend myself to try to make some code easier to write, more flexible and more complete in the future. For example, the generic client paging display helper class (aspnetpager) mentioned in this article is frequently used in large and small projects, however, it is obvious that the choice of paging mode and style control are weak. Although the previous implementation of this function is based on the needs of the actual project and has no impact on the old project, this cannot be the reason why the function is not fully scalable and universal, this has always been proud of the building pigs have to admit.
1. Paging mode and style Extension
(1) define Enumeration
Copy the 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>
/// Statistics paging Mode
/// </Summary>
Statistics = 2
}

(2) Rendering Based on enumeration
Copy the Code as follows:
/// <Summary>
/// Pagination category
/// </Summary>
Public sealed class aspnetpager
{
# Region common
Private const string defaultalign = "center"; // default alignment Mode
Private const string leftalign = "Left ";
Private const string rightalign = "right ";
/// <Summary>
/// Obtain the page number
/// </Summary>
/// <Param name = "objs"> total number of records </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>
/// Obtain the page number
/// </Summary>
/// <Param name = "totalcount"> total number of records </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 the page number (no table)
/// </Summary>
/// <Param name = "pagermode"> </param>
/// <Param name = "response"> </param>
/// <Param name = "basestring"> </param>
/// <Param name = "totalcount"> total number of records </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 recordcountperpage, 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 the page number (with tables)
/// </Summary>
/// <Param name = "pagermode"> </param>
/// <Param name = "alignenum"> </param>
/// <Param name = "response"> </param>
/// <Param name = "basestring"> </param>
/// <Param name = "totalcount"> total number of records </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>
/// Generate a paging string (the number of displayed pages is related to the number of records on each page)
/// </Summary>
/// <Param name = "basestring"> </param>
/// <Param name = "pagecount"> page number </param>
/// <Param name = "nowpage"> current page </param>
/// <Param name = "recordcountperpage"> Number of records per page (recommended number of records: 10) </param>
/// <Returns> </returns>
Private Static string createlinkurl (string basestring, int pagecount, int nowpage, int recordcountperpage)
{
Stringbuilder sb = new stringbuilder ();
Int from,;
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 \ "> homepage </a>", basestring ));
If (pagecount> 1 & nowpage> 1)
{
SB. appendformat ("<a href = \" {0} pageindex = {1} \ "> previous page </a>", basestring, (nowpage-1 ). tostring ());
}
Else
{
SB. append ("<a href = 'javascript: void (0); 'style = 'color: Gray; '> previous page </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 page </a>", basestring, pagecount ));
Return sb. tostring ();
}
/// <Summary>
/// Generate a paging string containing statistical information (the number of displayed pages is related to the number of records on each page)
/// </Summary>
/// <Param name = "basestring"> </param>
/// <Param name = "totalcount"> total number of records </param>
/// <Param name = "pagecount"> page number </param>
/// <Param name = "nowpage"> current page </param>
/// <Param name = "recordcountperpage"> Number of records per page (recommended number of records: 10) </param>
/// <Returns> </returns>
Private Static string createstatisticlinkurl (string basestring, int totalcount, int pagecount, int nowpage, int recordcountperpage)
{
Stringbuilder sb = new stringbuilder ();
String numricpager = createlinkurl (basestring, pagecount, nowpage, recordcountperpage); // pagination of common numbers
SB. appendformat ("Total <span style = 'color: red; '>{0} </span> records, total <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 table content alignment, almost all style controls in the Code are hard-coded. I personally think that the less control the style at the encoding stage, the better. Ideally, styles should be controlled externally, insteadProgramPersonnel to design and implement the solution. Here we will take a compromise based on the actual situation.
PS2: The page display mode can be expanded. For details, refer to the page display mode of the gridview control.
2. Calls on the page
Copy the 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">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> simple ASPnet paging </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<% DOTNET. Common. webform. aspnetpager. rendertablepager (pagermodoule. Normal, tbalignenum. Right, response, 93, 10, 10, "pager. aspx"); %>
</Div>
</Form>
</Body>
</Html>

3. Image and truth
(1) Normal Mode

(2) Statistical Mode

 

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.