闲暇时重新再看之前很多项目代码的时候,发现很多总是有那么点缺陷的代码,如芒刺入骨,令人心烦。挣扎良久,苦口婆心劝荐自己今后要争取把某些代码写的更易用更灵活更完善一些。比如在这篇已经提及到的通用客户端分页显示辅助类(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 name="objs">总记录数</param>
/// <param name="recordCountPerPage">每页记录数</param>
/// <returns></returns>
public static int GetPageCout(object[] objs, int recordCountPerPage)
{
return (int)Math.Ceiling(((double)objs.Length / (double)recordCountPerPage));
}
/// <summary>
/// 获取页数
/// </summary>
/// <param name="totalCount">总记录数</param>
/// <param name="recordCountPerPage">每页记录数</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 name="pagerMode"></param>
/// <param name="response"></param>
/// <param name="baseString"></param>
/// <param name="totalCount">总记录数</param>
/// <param name="nowPage">当前页数</param>
/// <param name="recordCountPerPage">每页记录数</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 name="pagerMode"></param>
/// <param name="alignEnum"></param>
/// <param name="response"></param>
/// <param name="baseString"></param>
/// <param name="totalCount">总记录数</param>
/// <param name="nowPage">当前页数</param>
/// <param name="recordCountPerPage">每页记录数</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>
/// 生成分页字符串(显示页数和每页记录数相关)
/// </summary>
/// <param name="baseString"></param>
/// <param name="pageCount">页数</param>
/// <param name="nowPage">当前页数</param>
/// <param name="recordCountPerPage">每页记录数(推荐记录数: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=\"{0}pageIndex=1\" >首页</a>", baseString));
if (pageCount > 1 && nowPage > 1)
{
sb.AppendFormat("<a href=\"{0}pageIndex={1}\" >上一页</a>", baseString, (nowPage - 1).ToString());
}
else
{
sb.Append("<a href='javascript:void(0);' style='color:gray;' >上一页</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}\" >下一页</a>", baseString, (nowPage + 1).ToString());
}
else
{
sb.Append("<a href=\"javascript:void(0);\" style='color:gray;' >下一页</a>");
}
sb.Append(string.Format(" <a href={0}pageIndex={1} >尾页</a>", baseString, pageCount));
return sb.ToString();
}
/// <summary>
/// 生成含统计信息的分页字符串(显示页数和每页记录数相关)
/// </summary>
/// <param name="baseString"></param>
/// <param name="totalCount">总记录数</param>
/// <param name="pageCount">页数</param>
/// <param name="nowPage">当前页数</param>
/// <param name="recordCountPerPage">每页记录数(推荐记录数: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 style='color:red;'>{0}</span>条记录,共<span style='color:red;'>{1}</span>页,当前第<span style='color:red;'>{2}</span>页 ",
totalCount, pageCount, nowPage);
sb.Append(numricPager);
return sb.ToString();
}
#endregion
}
ps1:除了表格内容对齐方式外,代码里对样式的控制几乎都是硬编码,个人认为编码阶段对样式的控制越少越好。理想状态下,样式应该让UI全部在外部控制,而不应该由程序员来设计实现,这里结合实际情况取折中方案。
ps2:分页显示的方式其实还可以扩展,具体请参考gridview控件分页模式。
2、页面内调用
复制代码 代码如下:
<%@ 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>aspnet简单分页</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、有图有真相
(1)、普通模式
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 name="objs">总记录数</param>
/// <param name="recordCountPerPage">每页记录数</param>
/// <returns></returns>
public static int GetPageCout(object[] objs, int recordCountPerPage)
{
return (int)Math.Ceiling(((double)objs.Length / (double)recordCountPerPage));
}
/// <summary>
/// 获取页数
/// </summary>
/// <param name="totalCount">总记录数</param>
/// <param name="recordCountPerPage">每页记录数</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 name="pagerMode"></param>
/// <param name="response"></param>
/// <param name="baseString"></param>
/// <param name="totalCount">总记录数</param>
/// <param name="nowPage">当前页数</param>
/// <param name="recordCountPerPage">每页记录数</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 name="pagerMode"></param>
/// <param name="alignEnum"></param>
/// <param name="response"></param>
/// <param name="baseString"></param>
/// <param name="totalCount">总记录数</param>
/// <param name="nowPage">当前页数</param>
/// <param name="recordCountPerPage">每页记录数</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>
/// 生成分页字符串(显示页数和每页记录数相关)
/// </summary>
/// <param name="baseString"></param>
/// <param name="pageCount">页数</param>
/// <param name="nowPage">当前页数</param>
/// <param name="recordCountPerPage">每页记录数(推荐记录数: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=\"{0}pageIndex=1\" >首页</a>", baseString));
if (pageCount > 1 && nowPage > 1)
{
sb.AppendFormat("<a href=\"{0}pageIndex={1}\" >上一页</a>", baseString, (nowPage - 1).ToString());
}
else
{
sb.Append("<a href='javascript:void(0);' style='color:gray;' >上一页</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}\" >下一页</a>", baseString, (nowPage + 1).ToString());
}
else
{
sb.Append("<a href=\"javascript:void(0);\" style='color:gray;' >下一页</a>");
}
sb.Append(string.Format(" <a href={0}pageIndex={1} >尾页</a>", baseString, pageCount));
return sb.ToString();
}
/// <summary>
/// 生成含统计信息的分页字符串(显示页数和每页记录数相关)
/// </summary>
/// <param name="baseString"></param>
/// <param name="totalCount">总记录数</param>
/// <param name="pageCount">页数</param>
/// <param name="nowPage">当前页数</param>
/// <param name="recordCountPerPage">每页记录数(推荐记录数: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 style='color:red;'>{0}</span>条记录,共<span style='color:red;'>{1}</span>页,当前第<span style='color:red;'>{2}</span>页 ",
totalCount, pageCount, nowPage);
sb.Append(numricPager);
return sb.ToString();
}
#endregion
}
ps1:除了表格内容对齐方式外,代码里对样式的控制几乎都是硬编码,个人认为编码阶段对样式的控制越少越好。理想状态下,样式应该让UI全部在外部控制,而不应该由程序员来设计实现,这里结合实际情况取折中方案。
ps2:分页显示的方式其实还可以扩展,具体请参考gridview控件分页模式。
2、页面内调用
复制代码 代码如下:
<%@ 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>aspnet简单分页</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、有图有真相
(1)、普通模式
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
暂无评论...
更新日志
2024年11月29日
2024年11月29日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]