分页功能实现方面参考了网上的一些内容,加上自己的一些修改,使用方式如 下:
(鉴于分页代码基本上和别人的没什么两样,所以这里就省略了。)
封装扩展方法
public static string Pager(this HtmlHelper html, IPageData model, bool alwaysShow,bool showNumber) { string text = "<!--没有任何数据,所以不显示分页控件-->"; bool show = model.TotalCount > 0 && alwaysShow || model.TotalCount > model.PageSize; if (show) { StringBuilder sb = new StringBuilder(); var reqUrl = html.ViewContext.RequestContext.HttpContext.Request.Url.ToString(); string link = ""; if (reqUrl.IndexOf("page") >= 0) { Regex re = new Regex(@"page=(\d+)", RegexOptions.IgnoreCase); link = re.Replace(reqUrl, "page=%7Bpage%7D"); } else if (reqUrl.IndexOf("?") < 0) { link = reqUrl + "?page=%7Bpage%7D"; } else { link = reqUrl + "&page=%7Bpage%7D"; } #region 生成四个按钮 TagBuilder root = new TagBuilder("div"); root.AddCssClass("ZuiePager"); TagBuilder fBtn = new TagBuilder("a"); fBtn.AddCssClass("btn"); fBtn.MergeAttribute("style", "font-family:Webdings;"); fBtn.InnerHtml = "9"; TagBuilder pBtn = new TagBuilder("a"); pBtn.AddCssClass("btn"); pBtn.MergeAttribute("style", "font-family:Webdings;"); pBtn.InnerHtml = "3"; TagBuilder nBtn = new TagBuilder("a"); nBtn.AddCssClass("btn"); nBtn.MergeAttribute("style", "font-family:Webdings;"); nBtn.InnerHtml = "4"; TagBuilder lBtn = new TagBuilder("a"); lBtn.AddCssClass("btn"); lBtn.MergeAttribute("style", "font-family:Webdings;"); lBtn.InnerHtml = ":"; #endregion int p = model.PageIndex; #region 判断首尾页 if (model.HasPreviousPage) { string pUrl = link.Replace("%7Bpage%7D", (p - 1).ToString()); string fUrl = link.Replace("%7Bpage%7D", (1).ToString()); fBtn.MergeAttribute("href", fUrl); pBtn.MergeAttribute("href", pUrl); } else { fBtn.MergeAttribute("disabled", "disabled"); pBtn.MergeAttribute("disabled", "disabled"); } if (model.HasNextPage) { string nUrl = link.Replace("%7Bpage%7D", (p + 1).ToString()); string lUrl = link.Replace("%7Bpage%7D", model.TotalPages.ToString()); nBtn.MergeAttribute("href", nUrl); lBtn.MergeAttribute("href", lUrl); } else { nBtn.MergeAttribute("disabled", "disabled"); lBtn.MergeAttribute("disabled", "disabled"); } #endregion sb.Append(fBtn.ToString()); sb.Append(pBtn.ToString()); #region 生成中间数字 if (showNumber) { int begin = p - 5; int end = p + 5; if (begin < 1) { begin = 1; end = 11; } if (end > model.TotalPages) { end = model.TotalPages; begin = end > 11 ? end - 10 : 1; } for (int i = begin; i <= end; i++) { TagBuilder num = new TagBuilder("span"); if (p == i) { num.AddCssClass("current"); num.InnerHtml = i.ToString(); } else { string url = link.Replace("%7Bpage%7D", i.ToString()); num.AddCssClass("num"); TagBuilder a = new TagBuilder("a"); a.MergeAttribute("href", url); a.InnerHtml = i.ToString(); num.InnerHtml = a.ToString()+" "; } sb.Append(num.ToString()); } } #endregion sb.Append(nBtn.ToString()); sb.Append(lBtn.ToString()); #region 生成页信息 TagBuilder index = new TagBuilder("span"); index.AddCssClass("num"); index.InnerHtml = p.ToString(); TagBuilder pagecount = new TagBuilder("span"); pagecount.AddCssClass("num"); pagecount.InnerHtml = model.TotalPages.ToString(); TagBuilder pagesize = new TagBuilder("span"); pagesize.AddCssClass("num"); pagesize.InnerHtml = model.PageSize.ToString(); TagBuilder count = new TagBuilder("span"); count.AddCssClass("num"); count.InnerHtml = model.TotalCount.ToString(); sb.Append(string.Format(" 第{0}页/共{1}页 每页{2}条记录 合计{3}条", index.ToString(), pagecount.ToString(), pagesize.ToString(), count.ToString())); root.InnerHtml = sb.ToString(); text = root.ToString(); #endregion } return text; }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索string
, tostring
, link
, model
, disabled
, Asp.Mvc
, MVC分页控件
mergeAttributes
asp.net mvc 分页控件、mvc 分页控件、mvc 自己写的分页控件、mvc5 分页控件、mvcpager分页控件,以便于您获取更多的相关知识。
时间: 2024-11-02 19:12:09