In my spare time, when I looked at a lot of previous project code again, I found many code that always had such a point of defect, for example, it was annoying. 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 codeThe Code is 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 codeThe Code is 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 allow all the UIS to be externally controlled, instead of being designed and implemented by programmers. 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 codeThe Code is 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
Finally, we look forward to your suggestions and comments.
Package and download demo files