闲暇时重新再看之前很多项目代码的时候,发现很多总是有那么点缺陷的代码,如芒刺入骨,令人心烦。挣扎良久,苦口婆心劝荐自己今后要争取把某些代码写的更易用更灵活更完善一些。比如在这篇已经提及到的通用客户端分页显示辅助类(AspNetPager),在大大小小项目中出现频率非常高,但是显然对分页形式的选择和样式的控制弱了点。虽然之前这个功能的实现都是按照实际项目的需求来实现的,对旧项目没有影响,但是这也不能成为该功能没有充分实现扩展性和通用性的理由,这点向来自傲的楼猪也不得不承认。
1、分页模式和样式扩展
(1)定义枚举
复制代码 代码如下:
/// <summary>
/// 表格对齐枚举
/// </summary>
public enum TbAlignEnum
{
Left = 1,
Center = 2,
Right = 3
}
/// <summary>
/// 分页模式
/// </summary>
public enum PagerModoule
{
/// <summary>
/// 普通分页模式
/// </summary>
Normal = 1,
/// <summary>
/// 统计分页模式
/// </summary>
Statistics = 2
}
(2)根据枚举呈现
复制代码 代码如下:
/// <summary>
/// 分页实用类
/// </summary>
public sealed class AspNetPager
{
#region common
private const string defaultAlign = "center";//默认对齐方式
private const string leftAlign = "left";
private const string rightAlign = "right";
/// <summary>
/// 获取页数
/// </summary>
/// <param>总记录数</param>
/// <param>每页记录数</param>
/// <returns></returns>
public static int GetPageCout(object[] objs, int recordCountPerPage)
{
return (int)Math.Ceiling(((double)objs.Length / (double)recordCountPerPage));
}
/// <summary>
/// 获取页数
/// </summary>
/// <param>总记录数</param>
/// <param>每页记录数</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>
/// 写分页页码(没有表格)
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param>总记录数</param>
/// <param>当前页数</param>
/// <param>每页记录数</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>
/// 写分页页码(有表格)
/// </summary>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param></param>
/// <param>总记录数</param>
/// <param>当前页数</param>
/// <param>每页记录数</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><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>
/// 生成分页字符串(显示页数和每页记录数相关)
/// </summary>
/// <param></param>
/// <param>页数</param>
/// <param>当前页数</param>
/// <param>每页记录数(推荐记录数:10)</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=https://www.jb51.net/article/\"{0}pageIndex=1\" >首页</a>", baseString));
if (pageCount > 1 && nowPage > 1)
{
sb.AppendFormat("<a href=https://www.jb51.net/article/\"{0}pageIndex={1}\" >上一页</a>", baseString, (nowPage - 1).ToString());
}
else
{
sb.Append("<a href='javascript:void(0);' >上一页</a>");
}
for (int i = from; i <= to; i++)
{
if (i == nowPage)
{
sb.AppendFormat(" <a href='javascript:void(0);' >{0}</a>", nowPage.ToString());
}
else
{
sb.AppendFormat(" <a href=https://www.jb51.net/article/\"{0}pageIndex={1}\" >{1}</a>", baseString, i);
}
}
if (pageCount > 1 && nowPage < pageCount)
{
sb.AppendFormat("<a href=https://www.jb51.net/article/\"{0}pageIndex={1}\" >下一页</a>", baseString, (nowPage + 1).ToString());
}
else
{
sb.Append("<a href=https://www.jb51.net/article/\"javascript:void(0);\" >下一页</a>");
}
sb.Append(string.Format(" <a href=https://www.jb51.net/{0}pageIndex={1} >尾页</a>", baseString, pageCount));
return sb.ToString();
}
/// <summary>
/// 生成含统计信息的分页字符串(显示页数和每页记录数相关)
/// </summary>
/// <param></param>
/// <param>总记录数</param>
/// <param>页数</param>
/// <param>当前页数</param>
/// <param>每页记录数(推荐记录数: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);//普通数字分页
sb.AppendFormat("总共<span>{0}</span>条记录,共<span>{1}</span>页,当前第<span>{2}</span>页 ",
totalCount, pageCount, nowPage);
sb.Append(numricPager);
return sb.ToString();
}
#endregion
}
ps1:除了表格内容对齐方式外,代码里对样式的控制几乎都是硬编码,个人认为编码阶段对样式的控制越少越好。理想状态下,样式应该让UI全部在外部控制,而不应该由程序员来设计实现,这里结合实际情况取折中方案。
ps2:分页显示的方式其实还可以扩展,具体请参考gridview控件分页模式。
2、页面内调用
复制代码 代码如下: