MVC HtmlHelper扩展类PagingHelper实现分页功能,供大家参考,具体内容如下
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace HtmlHelperMvc.Models { /// <summary> /// 分页类如果一个页面显示两个列表只需要复制该类到项目中重命名一个就可以 /// </summary> public static class PagingHelper { #region 属性Property /// <summary> /// 当前页码 /// </summary> private static int".class值").live("click", function () { /// var page = $(this).attr("pagingParamName值"); /// $("#order").html("").load("/Customer/Order"+page); /// });live自动给遍历增加事件 /// </summary> /// <param name="html"></param> /// <param name="htmlAttributes">new {@class="grey",pagingParamName="page",href="/Admin/Product/Index" rel="external nofollow" } pagingParamName默认page,匿名类添加控件属性</param> /// <returns></returns> public static MvcHtmlString Paging(this System.Web.Mvc.HtmlHelper html, object htmlAttributes) { RouteValueDictionary values = new RouteValueDictionary(htmlAttributes); #region 属性赋值 if (values["href"] != null) { PagingUrl = values["href"].ToString(); } if (values["pagingParamName"] != null) { PagingParamName = values["pagingParamName"].ToString(); values.Remove("pagingParamName"); } else { PagingParamName = "page"; } #endregion #region 分页最外层div/span TagBuilder builder = new TagBuilder("div");//span //创建Id,注意要先设置IdAttributeDotReplacement属性后再执行GenerateId方法. //builder.IdAttributeDotReplacement = "_"; //builder.GenerateId(id); //builder.AddCssClass(""); //builder.MergeAttributes(values); builder.InnerHtml = PagingBuilder(values); #endregion return MvcHtmlString.Create(builder.ToString(TagRenderMode.Normal));//解决直接显示html标记 } private static string PagingBuilder(RouteValueDictionary values) { #region 条件搜索时包括其他参数 StringBuilder urlParameter = new StringBuilder(); NameValueCollection collection = HttpContext.Current.Request.QueryString; string[] keys = collection.AllKeys; for (int i = 0; i < keys.Length; i++) { if (keys[i].ToLower() != "page") { urlParameter.AppendFormat("&{0}={1}", keys[i], collection[keys[i]]); } } #endregion //CurrentPage = Convert.ToInt32(HttpContext.Current.Request.QueryString["page"] "0"); StringBuilder sb = new StringBuilder(); #region 分页统计 sb.AppendFormat("Total {0} Records Page {1} of {2} ", TotalCount, CurrentPage, TotalPages); #endregion #region 首页 上一页 sb.AppendFormat(TagBuilder(values, 1, " First")); //sb.AppendFormat("<a href={0}",url,urlParameter); if (HasPreviousPage) { sb.AppendFormat(TagBuilder(values, CurrentPage - 1, " Prev ")); //sb.AppendFormat("<a href={0}", url, CurrentPage - 1, urlParameter); } #endregion #region 分页逻辑 if (TotalPages > 10) { if ((CurrentPage + 5) < TotalPages) { if (CurrentPage > 5) { for (int i = CurrentPage - 5; i <= CurrentPage + 5; i++) { sb.Append(TagBuilder(values, i, i.ToString())); } } else { for (int i = 1; i <= 10; i++) { sb.Append(TagBuilder(values, i, i.ToString())); } } sb.Append("... "); } else { for (int i = CurrentPage - 10; i <= TotalPages; i++) { sb.Append(TagBuilder(values, i, i.ToString())); } } } else { for (int i = 1; i <= TotalPages; i++) { sb.Append(" " + TagBuilder(values, i, i.ToString()) + " "); } } #endregion #region 下一页 末页 if (HasNextPage) { sb.AppendFormat(TagBuilder(values, CurrentPage + 1, "Next")); //sb.AppendFormat("<a href={0}", url, CurrentPage + 1, urlParameter); } sb.AppendFormat(TagBuilder(values, TotalPages, "Last")); //sb.AppendFormat("<a href={0}",url,TotalPages,urlParameter); #endregion return sb.ToString(); } private static string TagBuilder(RouteValueDictionary values, int i, string innerText) { values[PagingParamName] = i; TagBuilder tag = new TagBuilder("a"); if (PagingUrl != null) { values["href"] = PagingUrl + "" + PagingParamName + "= " + i + " "; } if (CurrentPage == i && innerText != " First" && innerText != " Last") { values["id"] = "on"; } else { tag.Attributes["id"] = ""; } tag.MergeAttributes(values); tag.SetInnerText(innerText); return tag.ToString(); } #endregion } }
后台Controller代码
// // GET: /Home/ public ActionResult Index(int"htmlcode">@{ ViewBag.Title = "Index"; } @using HtmlHelperMvc.Models; <h2>Index</h2> <hr /> <style type="text/css"> #on { color: #FFF; background-color: #337AB7; border-color: #337AB7; } .pagination a { margin-right: 3px; padding: 5px 10px; font-size: 12px; text-decoration: none; background-color: #fff; border: 1px solid #ddd; cursor: pointer; display: inline-block; border-radius: 3px; } a { color: #337ab7; text-decoration: none; } a { background-color: transparent; } * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } </style> <script src="/UploadFiles/2021-04-02/jquery-1.8.2.js">最终效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]