程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> 關於ASP.NET >> 一個簡單的ASP.MVC分頁控件

一個簡單的ASP.MVC分頁控件

編輯:關於ASP.NET

分頁功能實現方面參考了網上的一些內容,加上自己的一些修改,使用方式如 下:

(鑒於分頁代碼基本上和別人的沒什麼兩樣,所以這裡就省略了。)

封裝擴展方法

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;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved