分页功能实现方面参考了网上的一些内容,加上自己的一些修改,使用方式如 下:
(鉴于分页代码基本上和别人的没什么两样,所以这里就省略了。)
封装扩展方法
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") |