服务器之家

服务器之家 > 正文

asp.net 通用分页显示辅助类(改进版)

时间:2019-09-02 09:41     来源/作者:脚本之家
闲暇时重新再看之前很多项目代码的时候,发现很多总是有那么点缺陷的代码,如芒刺入骨,令人心烦。挣扎良久,苦口婆心劝荐自己今后要争取把某些代码写的更易用更灵活更完善一些。比如在这篇已经提及到的通用客户端分页显示辅助类(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)、普通模式

asp.net 通用分页显示辅助类(改进版)
(2)、带统计模式
asp.net 通用分页显示辅助类(改进版)
最后,欢迎补充,期待您的建议和意见。

演示文件打包下载

标签:

相关文章

热门资讯

华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字 2019-06-05
叠猫猫队长可以退出吗 叠猫猫队长怎么换队伍
叠猫猫队长可以退出吗 叠猫猫队长怎么换队伍 2019-06-05
逃跑吧少年怎么卡皮肤 逃跑吧少年卡永久皮肤技巧
逃跑吧少年怎么卡皮肤 逃跑吧少年卡永久皮肤技巧 2019-06-21
返回顶部