mvc3分页封装
1.先自定義一個類為PagingInfo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace eBuul.ChinaJoy.BLL.Models
{
??? public class PagingInfo
??? {
??????? /// <summary>
??????? /// 總條數(shù)
??????? /// </summary>
??????? public int TotalItems { get; set; }
??????? /// <summary>
??????? /// 每頁條數(shù)
??????? /// </summary>
??????? public int ItemsPerPage { get; set; }
??????? /// <summary>
??????? /// 當(dāng)前頁數(shù)
??????? /// </summary>
??????? public int CurrentPage { get; set; }
??????? /// <summary>
??????? /// 總頁數(shù)
??????? /// </summary>
??????? public int TotalPages
??????? {
??????????? get
??????????? {
??????????????? if (ItemsPerPage!=0)
??????????????? {
??????????????????? return (int)Math.Ceiling((decimal)TotalItems / ItemsPerPage);?
??????????????? }
??????????????? return 0;
???????????????
??????????????
??????????? }
??????? }
??? }
}
2.在Action中寫入以下代碼:
PagingInfo pagingInfo = new PagingInfo() { CurrentPage = pageIndex, ItemsPerPage = pageSize, TotalItems = total };
ViewBag.PagingInfo = pagingInfo;
3.在view視圖中調(diào)用
? <div class="pages">
??????????? @Html.PageLinkes("FineGame", "Index", (eBuul.ChinaJoy.BLL.Models.PagingInfo)ViewBag.PagingInfo, new string[] { "Keyword" })
??????? </div>
4.Html擴展方法實現(xiàn)
?
?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using eBuul.ChinaJoy.BLL.Models;
namespace System.Web.Mvc.Html
{
??? public static class PagingHelpers
??? {
??????? public static MvcHtmlString PageLinkes(this HtmlHelper html, string controller, string ActionName, PagingInfo pagingInfo, string[] paramArray)
??????? {
??????????? HttpContext contenxt = HttpContext.Current;
??????????? if (pagingInfo.TotalItems==0)
??????????? {
??????????????? return MvcHtmlString.Create("");
??????????? }
??????????? //設(shè)置分頁顯示的區(qū)間值
??????????? int IsNextPage = 1;
??????????? int currentPage = (pagingInfo.CurrentPage - 1) / 5;
??????????? int start = 1, end = 5;
??????????? if (currentPage >= 1)
??????????? {
??????????????? start = (currentPage * 5) + 1;
??????????????? end = (currentPage + 1) * 5;
??????????? }
??????????? StringBuilder result = new StringBuilder();
??????????? string tmp = "";
??????????? if (paramArray != null && paramArray.Length > 0)
??????????? {
??????????????? for (int n = 0; n < paramArray.Length; n++)
??????????????? {
??????????????????? if (contenxt.Request.Params.AllKeys.Contains(paramArray[n]))
??????????????????????? tmp += "&" + paramArray[n] + "=" + contenxt.Request.Params[paramArray[n]];
??????????????? }
??????????? }
??????????? //生成中間的分頁數(shù)目
??????????? int i = 0;
??????????? for (i = start; i <= end; i++)
??????????? {
??????????????? if (i <= pagingInfo.TotalPages)
??????????????? {
??????????????????? TagBuilder litag = new TagBuilder("li");
??????????????????? TagBuilder tag = new TagBuilder("a");
??????????????????? if (tmp != "")
??????????????????????? tag.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, i, tmp));
??????????????????? else
??????????????????????? tag.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, i));
??????????????????? tag.InnerHtml = i.ToString();
??????????????????? if (i == pagingInfo.CurrentPage)
??????????????????????? litag.AddCssClass("li_bg");
??????????????????? litag.InnerHtml = tag.ToString();
??????????????????? result.Append(litag.ToString());
??????????????????? IsNextPage++;
??????????????? }
??????????????? else
??????????????????? break;
??????????? }
??????????? //設(shè)置分頁顯示的下一區(qū)間值的起始位置(即頁面中的"..."按鈕)
??????????? if (pagingInfo.TotalPages > 5 && (i - 1) != pagingInfo.TotalPages)
??????????? {
??????????????? TagBuilder litagDot = new TagBuilder("li");
??????????????? TagBuilder tagDot = new TagBuilder("a");
??????????????? if (tmp != "")
??????????????????? tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, end + 1, tmp));
??????????????? else
??????????????????? tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, end + 1));
??????????????? tagDot.InnerHtml = "...";
??????????????? litagDot.InnerHtml = tagDot.ToString();
??????????????? result.Append(litagDot.ToString());
??????????? }
??????????? //設(shè)置分頁顯示的上一區(qū)間值的起始位置(即頁面中的"..."按鈕)
??????????? if (currentPage >= 1)
??????????? {
??????????????? TagBuilder litagDot = new TagBuilder("li");
??????????????? TagBuilder tagDot = new TagBuilder("a");
??????????????? if (tmp != "")
??????????????????? tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, start - 1, tmp));
??????????????? else
??????????????????? tagDot.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, start - 1));
??????????????? tagDot.InnerHtml = "...";
??????????????? litagDot.InnerHtml = tagDot.ToString();
??????????????? result.Insert(0, litagDot.ToString());
??????????? }
??????????? //上一頁
??????????? if (pagingInfo.TotalPages > 1)
??????????? {
??????????????? TagBuilder litagPre = new TagBuilder("li");
??????????????? TagBuilder tagPre = new TagBuilder("a");
??????????????? //TagBuilder tagImgPre = new TagBuilder("img");
??????????????? //tagImgPre.MergeAttribute("src", "/Content/imghome/left02.png");
??????????????? if (pagingInfo.CurrentPage == 1)
??????????????????? tagPre.MergeAttribute("href", "#");
??????????????? else
??????????????? {
??????????????????? if (tmp != "")
??????????????????????? tagPre.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, pagingInfo.CurrentPage - 1, tmp));
??????????????????? else
??????????????????????? tagPre.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, pagingInfo.CurrentPage - 1));
??????????????? }
??????????????? //tagPre.InnerHtml = tagImgPre.ToString();
?????????????
??????????????????? tagPre.InnerHtml = "上一頁";
??????????????? litagPre.InnerHtml = tagPre.ToString();
??????????????? result.Insert(0, litagPre);
??????????? }
??????????? //下一頁
??????????? if (pagingInfo.TotalPages > 1)
??????????? {
??????????????? TagBuilder litagNext = new TagBuilder("li");
??????????????? TagBuilder tagNext = new TagBuilder("a");
??????????????? //TagBuilder tagImgNext = new TagBuilder("img");
??????????????? //tagImgNext.MergeAttribute("src", "/Content/imghome/right02.png");
??????????????? if (pagingInfo.CurrentPage == pagingInfo.TotalPages)
??????????????????? tagNext.MergeAttribute("href", "#");
??????????????? else
??????????????? {
??????????????????? if (tmp != "")
??????????????????????? tagNext.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}{3}", controller, ActionName, pagingInfo.CurrentPage + 1, tmp));
??????????????????? else
??????????????????????? tagNext.MergeAttribute("href", string.Format("/{0}/{1}?pageNo={2}", controller, ActionName, pagingInfo.CurrentPage + 1));
??????????????? }
??????????????? //tagNext.InnerHtml = tagImgNext.ToString();
????????????
????????????????
??????????????????? tagNext.InnerHtml = "下一頁";
????????????
??????????????? litagNext.InnerHtml = tagNext.ToString();
??????????????? result.Append(litagNext.ToString());
??????????? }
??????????? //總頁數(shù)
??????????? TagBuilder litagTotal = new TagBuilder("li");
??????????? TagBuilder spantag = new TagBuilder("span");
??????????? litagTotal.AddCssClass("li_none");
??????????? spantag.InnerHtml = pagingInfo.TotalItems.ToString();
??????????? string paramUrl =? string.Format("/{0}/{1}{2}", controller, ActionName, string.IsNullOrWhiteSpace(tmp)?string.Empty:"?"+tmp.TrimStart('&'));
??????????? string pageHtml = string.Format("共<span id=\"pageSum\">{0}</span>頁 {1}條記錄 到第<input type=\"text\" size=\"2\" id=\"jumpto\" name=\"jumpto\" class=\"inputpage\" title=\"指定頁碼\" value=\"{3}\">頁<input type=\"button\" title=\"指定頁碼\" value=\"確定\" id=\"btnGO\" class=\"button_li\"><input type=\"hidden\" value=\"{2}\" id=\"hdnParamUrl\"", pagingInfo.TotalPages, spantag.ToString(), paramUrl, pagingInfo.TotalPages == pagingInfo.CurrentPage ? pagingInfo.CurrentPage : pagingInfo.CurrentPage+1);
?????????? litagTotal.InnerHtml = pageHtml;
??????????? result.Append(litagTotal.ToString());
??????????? TagBuilder ultag = new TagBuilder("ul");
??????????? ultag.InnerHtml = result.ToString();
??????????? return MvcHtmlString.Create(ultag.ToString());
??????? }
??? }
}
轉(zhuǎn)載于:https://www.cnblogs.com/yxlblogs/archive/2013/04/24/3040737.html
總結(jié)
- 上一篇: 第一个 python for maya
- 下一篇: c字符串函数实现(1)---strncp