当前位置:Gxlcms > asp.net > 简单好用的ASP.NET分页类(支持AJAX、自定义文字)

简单好用的ASP.NET分页类(支持AJAX、自定义文字)

时间:2021-07-01 10:21:17 帮助过:10人阅读

在做网站没用 JS UI控件时 很实用

用法:

  1. var ps=new PageString();
  2. /*可选参数*/
  3. ps.SetIsEnglish = true;// 是否是英文 (默认:false)
  4. ps.SetIsShowText = true;//是否显示分页文字 (默认:true)
  5. //ps.TextFormat="" (默认值:《span class=\"pagetext\"》《strong》总共《/strong》:{0} 条 《strong》当前《/strong》:{1}/{2}《/span》)
  6. //ps.SetPageIndexName Request["pageIndex"](默认值:"pageIndex")
  7. ps.SetIsAjax = false;// (默认值:"false")
  8. /*函数参数*/
  9. int total = 10000;
  10. int pageSize = 10;
  11. int pageIndex = Convert.ToInt32(Request["pageIndex"]);
  12. var page = ps.ToString(total, pageSize, pageIndex, "/UI/PageStringTest.aspx?");
  13. //获取 page html
输出 Response.Write(page);

效果:

代码:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. namespace SyntacticSugar
  7. {
  8. /// <summary>
  9. /// ** 描述:分页类
  10. /// ** 创始时间:2015-5-29
  11. /// ** 修改时间:-
  12. /// ** 作者:sunkaixuan
  13. public class PageString
  14. {
  15. /// <summary>
  16. /// 是否是英文 (默认:false)
  17. /// </summary>
  18. public bool SetIsEnglish { get; set; }
  19. /// <summary>
  20. /// 是否显示分页文字(默认:true)
  21. /// </summary>
  22. public bool SetIsShowText { get; set; }
  23. /// <summary>
  24. /// 样式 (默认:"pagination")
  25. /// </summary>
  26. public string SetClassName { get; set; }
  27. /// <summary>
  28. /// 分页参数名 (默认:"pageIndex")
  29. /// </summary>
  30. public string SetPageIndexName { get; set; }
  31. /// <summary>
  32. /// 是否是异步 同步 href='' 异步 onclick=ajaxPage() (默认:false)
  33. /// </summary>
  34. public bool SetIsAjax { get; set; }
  35. /// <summary>
  36. /// 自定义文字
  37. /// string.Format("{0}{1}{2}","总记录数","当前页数","总页数")
  38. /// 默认值:《span class=\"pagetext\"》《strong》总共《/strong》:{0} 条 《strong》当前《/strong》:{1}/{2}《/span》
  39. /// </summary>
  40. public string SetTextFormat { get; set; }
  41. public PageString()
  42. {
  43. SetIsEnglish = false;
  44. SetIsShowText = true;
  45. SetTextFormat = "<span class=\"pagetext\"><strong>总共</strong>:{0} 条 <strong>当前</strong>:{1}/{2}</span> ";
  46. SetClassName = "pagination";
  47. SetPageIndexName = "pageIndex";
  48. SetIsAjax = false;
  49. }
  50. /*免费的样式
  51. .pagination .click {cursor:pointer}
  52. .pagination a{text-decoration: none;border: 1px solid #AAE;color: #15B;font-size: 13px;border-radius: 2px;}
  53. .pagination span{ color:#666;font-size:13px;display: inline-block;border: 1px solid #ccc;padding: 0.2em 0.6em;}
  54. .pagination span.pagetext{ border:none}
  55. .pagination a:hover{background: #26B;color: #fff;}
  56. .pagination a{display: inline-block;padding: 0.2em 0.6em;}
  57. .pagination .page_current{background: #26B;color: #fff;border: 1px solid #AAE;margin-right: 5px;}
  58. .pagination{margin-top: 20px;}
  59. .pagination .current.prev, .pagination .current.next{color: #999;border-color: #999;background: #fff;}
  60. * */
  61. /// <summary>
  62. /// 分页算法<一>共20页 首页 上一页 1 2 3 4 5 6 7 8 9 10 下一页 末页
  63. /// </summary>
  64. /// <param name="total">总记录数</param>
  65. /// <param name="pageSize">每页记录数</param>
  66. /// <param name="pageIndex">当前页数</param>
  67. /// <param name="query_string">Url参数</param>
  68. /// <returns></returns>
  69. public string ToString(int total, int pageSize, int pageIndex, string query_string)
  70. {
  71. int allpage = 0;
  72. int next = 0;
  73. int pre = 0;
  74. int startcount = 0;
  75. int endcount = 0;
  76. StringBuilder pagestr = new StringBuilder();
  77. pageIndex = pageIndex == 0 ? 1 : pageIndex;
  78. pagestr.AppendFormat("<div class=\"{0}\" >", SetClassName);
  79. if (pageIndex < 1) { pageIndex = 1; }
  80. //计算总页数
  81. if (pageSize != 0)
  82. {
  83. allpage = (total / pageSize);
  84. allpage = ((total % pageSize) != 0 ? allpage + 1 : allpage);
  85. allpage = (allpage == 0 ? 1 : allpage);
  86. }
  87. next = pageIndex + 1;
  88. pre = pageIndex - 1;
  89. startcount = (pageIndex + 5) > allpage ? allpage - 9 : pageIndex - 4;//中间页起始序号
  90. //中间页终止序号
  91. endcount = pageIndex < 5 ? 10 : pageIndex + 5;
  92. if (startcount < 1) { startcount = 1; } //为了避免
输出的时候产生负数,设置如果小于1就从序号1开始 if (allpage < endcount) { endcount = allpage; }//页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 bool isFirst = pageIndex == 1; bool isLast = pageIndex == allpage; if (SetIsShowText) pagestr.AppendFormat(SetTextFormat, total, pageIndex, allpage); if (isFirst) { pagestr.Append("<span>首页</span> <span>上一页</span>"); } else { pagestr.AppendFormat("<a href=\"{0}pageIndex=1\">首页</a> <a href=\"{0}pageIndex={1}\">上一页</a>", query_string, pre); } //中间页处理,这个增加时间复杂度,减小空间复杂度 for (int i = startcount; i <= endcount; i++) { bool isCurent = pageIndex == i; if (isCurent) { pagestr.Append(" <a class=\"page_current\">" + i + "</a>"); } else { pagestr.Append(" <a href=\"" + query_string + "pageIndex=" + i + "\">" + i + "</a>"); } } if (isLast) { pagestr.Append("<span>下一页</span> <span>末页</span>"); } else { pagestr.Append(" <a href=\"" + query_string + "pageIndex=" + next + "\">下一页</a> <a href=\"" + query_string + "pageIndex=" + allpage + "\">末页</a>"); } pagestr.AppendFormat("</div>"); return ConversionData(pagestr.ToString()); } private string ConversionData(string page) { if (SetIsEnglish) { page= page.Replace("上一页", "Previous").Replace("下一页", "Next").Replace("总共", "total").Replace("当前", "Current").Replace("条", "records").Replace("首页", "First").Replace("末页", "Last"); } if (SetIsAjax) { var matches = Regex.Matches(page, @"href\="".*?""",RegexOptions.Singleline); if (matches != null && matches.Count > 0) { foreach (Match m in matches) { page = page.Replace(m.Value, string.Format("class=\"click\" onclick=\"ajaxPage('{0}')\"", Regex.Match(m.Value, string.Format(@"{0}\=(\d+)", SetPageIndexName)).Groups[1].Value)); } } } return page; } } }

人气教程排行