代码
public class Pager { #region property ////// 每一页的数据条数 /// public int PageSize { get; set; } ////// 总数据条数 /// public int TotalCount { get; set; } ////// 最多显示的页码数 /// public int MaxPagerCount { get; set; } ////// 当前页(从1开始) /// public int PageIndex { get; set; } ////// 拼接Html链接的格式,约定页码占位符{pn} /// 例如: "/Role/List?pageIndex={pn}" /// public string UrlPattern { get; set; } ////// 当前页码的样式名称 /// 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("
- "); for (int i = startPageIndex; i <= endPageIndex; i++) { if (i == PageIndex) { sb.AppendFormat("
- {1} ", CurrentPageClassName, i); } else { string href = UrlPattern.Replace("{pn}", i.ToString()); sb.AppendFormat("
- {1} ", href, i); } } sb.Append("