1,不再通过URL来取页面索引,而是通过action的参数,如{pageindex}来设置.
2,增加了传递url参数
3,提供了显示设置属性(如只显示"上一页","下一页"或者只显示"数字")
下面是两个属性设置类:
[System.Flags] public enum PageBarType : int { /// <summary> /// 数字 /// </summary> Number = 00000001, /// <summary> /// 第一页 /// </summary> First = 00000010, /// <summary> /// 最后一页 /// </summary> Last = 00000100, /// <summary> /// 上一页 /// </summary> Previous = 00001000, /// <summary> /// 下一页 /// </summary> Next = 00010000, /// <summary> /// 总计 /// </summary> Total = 00100000, /// <summary> /// 数字,第一页,最后一页 /// </summary> Number_First_Last = Number | First | Last, /// <summary> /// 数字,上一页,下一页 /// </summary> Number_Previous_Next = Number | Previous | Next, /// <summary> /// 上一页,下一页 /// </summary> Previous_Next = Previous | Next, /// <summary> /// 上一页,下一页,第一页,最后一页 /// </summary> Previous_Next_First_Last = Previous | Next | First | Last, /// <summary> /// 全部显示 /// </summary> Default = Number | Previous | Next | First | Last | Total } public struct PageBarSetting { public PageBarSetting(string first, string previousArea, string previous, string next, string nextarea, string last, int totalcount,int pagesize, int pages, int displaysize) { _first = first; _previousarea = previousArea; _previous = previous; _next = next; _nextarea = nextarea; _last = last; _totalcount = totalcount; _pages = pages; _pagesize = pagesize; _displaysize = displaysize; } public PageBarSetting(int totalcount,int pagesize, int pages, int displaysize) { _first = string.Empty; _previousarea = string.Empty; _previous = string.Empty; _next = string.Empty; _nextarea = string.Empty; _last = string.Empty; _totalcount = totalcount; _pagesize = pagesize; _pages = pages; if (_pagesize > 0) { //计算页数 _pages = _totalcount % _pagesize == 0 ? _totalcount / _pagesize : _totalcount / _pagesize + 1; } _displaysize = displaysize; Init(); } public PageBarSetting(bool isInit) { _first = string.Empty; _previousarea = string.Empty; _previous = string.Empty; _next = string.Empty; _nextarea = string.Empty; _last = string.Empty; _totalcount = 0; _pages = 0; _pagesize = 0; _displaysize = 0; if (isInit) { Init(); } } public void Init() { _first = "【第一页】"; _previousarea = "<<"; _previous = "【上一页】"; _next = "【下一页】"; _nextarea = ">>"; _last = "【最后一页】"; } #region = First = private string _first; public string First { get { return _first; } set { _first = value; } } #endregion #region = PreviousArea = private string _previousarea; public string PreviousArea { get { return _previousarea; } set { _previousarea = value; } } #endregion #region = Previous = private string _previous; public string Previous { get { return _previous; } set { _previous = value; } } #endregion #region = Next = private string _next; public string Next { get { return _next; } set { _next = value; } } #endregion #region = NextArea = private string _nextarea; public string NextArea { get { return _nextarea; } set { _nextarea = value; } } #endregion #region = Last = private string _last; public string Last { get { return _last; } set { _last = value; } } #endregion #region = TotalCount = private int _totalcount; public int TotalCount { get { return _totalcount; } set { _totalcount = value; } } #endregion #region = Pages = private int _pages; public int Pages { get { return _pages; } set { _pages = value; } } #endregion #region = PageSize = private int _pagesize; public int PageSize { get { return _pagesize; } set { _pagesize = value; } } #endregion #region = DisplaySize = private int _displaysize; public int DisplaySize { get { return _displaysize; } set { _displaysize = value; } } #endregion }
分页代码:
public static class PagerBarHelper { /// <summary> /// 分页HTML生成器 /// </summary> /// <param name="helper">待扩展的HtmlHelper</param> /// <param name="totalCount">数据总数</param> /// <param name="pageSize">页数</param> /// <param name="displaySize">分页控件允许显示的页数</param> /// <param name="htmlAttributes">html属性</param> /// <returns></returns> public static string PagerBar(this HtmlHelper helper, int totalCount,int pageSize, int displaySize, object htmlAttributes) { if (totalCount <= pageSize) return string.Empty; //计算页数 int pages = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1; return PagerBar(helper, htmlAttributes, totalCount, pages, displaySize); } /// <summary> /// 分页HTML生成器 /// </summary> /// <param name="helper">待扩展的HtmlHelper</param> /// <param name="htmlAttributes">html属性</param> /// <param name="totalCount">数据总数</param> /// <param name="pages">总页数</param> /// <param name="displaySize">分页控件允许显示的页数</param> /// <returns></returns> public static string PagerBar(this HtmlHelper helper, object htmlAttributes, int totalCount, int pages, int displaySize) { PageBarSetting pbSetting = new PageBarSetting(true); pbSetting.TotalCount = totalCount; pbSetting.Pages = pages; pbSetting.DisplaySize = displaySize; return PagerBar(helper, htmlAttributes, pbSetting, PageBarType.Previous_Next | PageBarType.First); } public static string PagerBar(this HtmlHelper helper, object htmlAttributes, PageBarSetting pbSetting, PageBarType pbType) { //只有一页数据,不显示分页 if (pbSetting.Pages < 2) return string.Empty; //string path = HttpContext.Current.Request.Url.AbsolutePath; //url参数 string query = HttpContext.Current.Request.Url.Query; //Regex reg = new Regex(@"^(.+?)/(/d*?)/?$", RegexOptions.IgnoreCase); //Match match = reg.Match(path); int currentIndex = Convert.ToInt32(helper.ViewContext.RouteData.Values["pageindex"]); System.Web.Routing.Route route = (System.Web.Routing.Route)helper.ViewContext.RouteData.Route; string urlPattern = route.Url.ToLower(); //将路由形式转化字符串格式化形式,{pageindex}转成{0} string urlmodel = "/" + urlPattern.Replace("pageindex", "0"); foreach (KeyValuePair<string, object> v in helper.ViewContext.RouteData.Values) { urlmodel = urlmodel.Replace(string.Format("{{{0}}}", v.Key.ToLower()), v.Value.ToString()); } //url模板 //string urlmodel = path.EndsWith("/") || path.EndsWith("//") ? path : path + "/"; //while (!match.Success) //{ // urlmodel += "1/"; // match = reg.Match(urlmodel); //} //if (match.Success) //{ // urlmodel = match.Groups[1].Value; // try // { // currentIndex = Convert.ToInt32(match.Groups[2].Value); // } // catch // { // //防止胡乱输入URL // } //} StringBuilder sBuilder = new StringBuilder(); sBuilder.Append("<span"); //HTML属性设置 if (htmlAttributes != null) { string t = htmlAttributes.GetType().ToString(); foreach (PropertyInfo pi in htmlAttributes.GetType().GetProperties()) { sBuilder.AppendFormat(" {0}=/"{1}/"", pi.Name, pi.GetValue(htmlAttributes, null)); } } sBuilder.Append(">"); if ((pbType | PageBarType.Total) == pbType) { if (pbSetting.TotalCount > 0) { sBuilder.AppendFormat("总共<label>{0}</label>条, ", pbSetting.TotalCount); } sBuilder.AppendFormat("当前<label>{0}</label>/<label>{1}</label>页,", currentIndex, pbSetting.Pages); } if (currentIndex > 1) { if ((pbType | PageBarType.First) == pbType) { sBuilder.AppendFormat(@"<a href=""{0}{1}"" target=""_self"">{2}</a>", string.Format(urlmodel, 1), query, pbSetting.First); } if ((pbType | PageBarType.Previous) == pbType) { sBuilder.AppendFormat(@"<a href=""{0}{1}"" target=""_self"">{2}</a>", string.Format(urlmodel, currentIndex - 1), query, pbSetting.Previous); } } #region =页码区域起始点= int areaStart = currentIndex % pbSetting.DisplaySize > 0 ? currentIndex / pbSetting.DisplaySize * pbSetting.DisplaySize + 1 : (currentIndex - 1) / pbSetting.DisplaySize * pbSetting.DisplaySize + 1; #endregion #region =显示上一个页码区间,并往前翻到区间末尾页= if (currentIndex > pbSetting.DisplaySize) { if ((pbType | PageBarType.Number) == pbType) { sBuilder.AppendFormat(@"<a href=""{0}{1}"" target=""_self"">{2}</a>", string.Format(urlmodel, areaStart - 1), query, pbSetting.PreviousArea); } } #endregion #region =计算分页需要显示的页码区域= if ((pbType | PageBarType.Number) == pbType) { for (int i = areaStart; i < areaStart + pbSetting.DisplaySize; i++) { //如果当前页超出总页数范围,则不再生成页码 if (i > pbSetting.Pages) break; if (i == currentIndex) { //当前页没有链接,设置标签 sBuilder.AppendFormat(@"<span><label>{0}</label></span>", i); } else { sBuilder.AppendFormat(@"<span><a href=""{0}{1}"" target=""_self"">{2}</a></span>", string.Format(urlmodel, i), query, i); } } } #endregion #region =显示下一个页码区间,并往后翻到区间首页= if (areaStart + pbSetting.DisplaySize <= pbSetting.Pages) { if ((pbType | PageBarType.Number) == pbType) { sBuilder.AppendFormat(@"<a href=""{0}{1}"" target=""_self"">{2}</a>", string.Format(urlmodel, areaStart + pbSetting.DisplaySize), query, pbSetting.NextArea); } } #endregion if (currentIndex < pbSetting.Pages) { if ((pbType | PageBarType.Next) == pbType) { sBuilder.AppendFormat(@"<a href=""{0}{1}"" target=""_self"">{2}</a>", string.Format(urlmodel, currentIndex + 1), query, pbSetting.Next); } if ((pbType | PageBarType.Last) == pbType) { sBuilder.AppendFormat(@"<a href=""{0}{1}"" target=""_self"">{2}</a>", string.Format(urlmodel, pbSetting.Pages), query,pbSetting.Last); } } sBuilder.Append("</span>"); return sBuilder.ToString(); }
css样式代码:
.PageBar2 { width: 578px; font-size: 12px; font-family: Tahoma; } .PageBar2 label { color: #3399FF; } .PageBar2 span { padding: 0px 3px 0px 3px; } .PageBar2 a:link, .PageBar2 a:visited { color: #629836; text-decoration:none; } .PageBar2 a:hover { color: #629836; text-decoration:underline; } .PageBar2 span a:link,.PageBar2 span a:visited { color: #629836; text-decoration:underline; } .PageBar2 span a:hover { background-color:#009900; color:#FFFFFF;} /* Current Page Number */ .PageBar2 span label { color: #3399FF; text-decoration:none; }
使用示例:
<%=Html.PagerBar(new { @class = "PageBar2" }, new PageBarSetting(1000, 10, 0, 5), PageBarType.Previous_Next)%>
PageBarType类型可自由组合设置,如
<%=Html.PagerBar(new { @class = "PageBar2" }, new PageBarSetting(1000, 10, 0, 10), PageBarType.Number_Previous_Next|PageBarType.First)%>