代码
public class Pager { #region property /// <summary> /// 每一页的数据条数 /// </summary> public int PageSize { get; set; } /// <summary> /// 总数据条数 /// </summary> public int TotalCount { get; set; } /// <summary> /// 最多显示的页码数 /// </summary> public int MaxPagerCount { get; set; } /// <summary> /// 当前页(从1开始) /// </summary> public int PageIndex { get; set; } /// <summary> /// 拼接Html链接的格式,约定页码占位符{pn} /// 例如: "/Role/List?pageIndex={pn}" /// </summary> public string UrlPattern { get; set; } /// <summary> /// 当前页码的样式名称 /// </summary> public string CurrentPageClassName { get; set; } #endregion public string GetPagerHtml() { if (TotalCount <= PageSize) return null; StringBuilder sb = new StringBuilder(); //ToDo:上一页,下一页,首页,末页 ,跳转页 //总页数 int pageCount = (int)Math.Ceiling(TotalCount * 1.0f / PageSize); //显示页码的 起始 页码 int startPageIndex = Math.Max(1, PageIndex - MaxPagerCount / 2); //显示页码的 结束 页码 int endPageIndex = Math.Min(pageCount, startPageIndex + MaxPagerCount - 1); sb.Append("<ul>"); for (int i = startPageIndex; i <= endPageIndex; i++) { if (i == PageIndex) { sb.AppendFormat("<li class=‘{0}‘>{1}</li>", CurrentPageClassName, i); } else { string href = UrlPattern.Replace("{pn}", i.ToString()); sb.AppendFormat("<li><a href=‘{0}‘>{1}</a></li>", href, i); } } sb.Append("</ul>"); return sb.ToString(); } }
原文:https://www.cnblogs.com/kikyoqiang/p/10828386.html