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

asp.net MVC 3.0 分頁控件

編輯:PHP綜合

asp.Net MVC 3.0 寫的分頁控件

效果如下

代碼如下

  1         /// <summary>
2 /// 分頁控件
3 /// </summary>
4 /// <param name="helper"></param>
5 /// <param name="PageSize">單頁條數</param>
6 /// <param name="CurrentPage">當前頁</param>
7 /// <param name="RecordCount">總記錄數</param>
8 /// <param name="Attribute">擴展參數</param>
9 /// <returns></returns>
10   public static MvcHtmlString Pagination(this HtmlHelper helper, Int32 PageSize, Int32 CurrentPage, Int32 RecordCount, Object Attribute)
11 {
12 /*
13 * <span>共:{RecordCount}條 {PageSize}條/頁 {CurrentPage}頁/{PageCount}頁</span> {List}
14 * <span>Page {CurrentPage} of {PageCount} ({RecordCount} items) PageSize:{PageSize}</span> {List}
15 * @VIEwType = 0, 0中文 1英文 2自定義中文 3自定義英文
16 * @Template = "<span>共:{RecordCount}條 {PageSize}條/頁 {CurrentPage}頁/{PageCount}頁</span> {List}",
17 * @Postfix = "",
18 * @VirtualPath = "",
19 * @IdentifIEr = "page",
20 * @Encrypt = false }
21 */
22
23 Int32 VIEwType = GetInt32(GetValue(Attribute, "VIEwType"));
24 String Template = GetString(GetValue(Attribute, "Template"));
25 String Postfix = GetString(GetValue(Attribute, "Postfix"));
26 String VirtualPath = GetString(GetValue(Attribute, "VirtualPath"));
27 String IdentifIEr = GetString(GetValue(Attribute, "IdentifIEr"));
28 Boolean Encrypt = GetBoolean(GetValue(Attribute, "Encrypt"));
29 Template = !String.IsNullOrEmpty(Template) ? String.Format("{0}{1}", "\n\t", SetString(Template)) :
30 VIEwType == 0 || VIEwType == 3 ? "\t<span>共:{RecordCount}條 {PageSize}條/頁 {CurrentPage}頁/{PageCount}頁</span> {List}" :
31 "\t<span>Page {CurrentPage} of {PageCount} ({RecordCount} items) PageSize:{PageSize}</span> {List}";
32
33 Int32 pagecount = (Int32)Math.Ceiling((Double)RecordCount / (Double)PageSize);
34 Int32 startPage = 0;
35 Int32 endPage = 0;
36
37 if (pagecount <= 10 || CurrentPage <= 3)
38 {
39 startPage = 1;
40 endPage = 10 > pagecount ? pagecount : 10;
41 }
42 else
43 {
44 if (pagecount - CurrentPage <= 7)
45 {
46 startPage = pagecount - 9;
47 endPage = pagecount;
48 }
49 else
50 {
51 startPage = CurrentPage - 2;
52 endPage = CurrentPage + 7;
53 }
54 }
55
56 Postfix = VIEwType == 0 || VIEwType == 1 ? "" : Postfix;
57 StringBuilder Html = new StringBuilder();
58 String CurrUrl = "";
59
60 if (VIEwType == 0 || VIEwType == 1)
61 {
62 CurrUrl = System.Web.HttpContext.Current.Request.RawUrl;
63 CurrUrl = DeleteUrlParameter(IdentifIEr);
64 IdentifIEr = IdentifIEr + "=";
65 }
66 else
67 {
68 CurrUrl = VirtualPath;
69 IdentifIEr = "";
70 }
71
72 if (CurrentPage > 1)
73 {
74 Html.AppendFormat("\n\t<a href=\"{0}{1}1\" title=\"{2}\"><<</a>\n\t<a href=\"{0}{1}{3}\" title=\"{4}\"><</a>\n",
75 CurrUrl,
76 IdentifIEr,
77 VIEwType == 0 || VIEwType == 3 ? "首頁" : "First",
78 CurrentPage - 1,
79 VIEwType == 0 || VIEwType == 3 ? "上一頁" : "Previous"
80 );
81 }
82
83 if (CurrentPage > 10)
84 {
85 Html.AppendFormat("\t<a href=\"{0}{1}{2}\" title=\"{3}\">....</a>\n",
86 CurrUrl,
87 IdentifIEr,
88 CurrentPage - 10,
89 VIEwType == 0 || VIEwType == 3 ? "上10頁" : "Previous Ten Pages"
90 );
91
92 }
93
94 for (Int32 j = startPage; j <= endPage; j++)
95 {
96 Html.Append(CurrentPage == j ? String.Format("\t<a class=\"curr\" href=\"Javascript:void(0)\">{0}</a> \n", j) : String.Format("\t<a href=\"{0}{1}{2}\" title=\"{2}\">{2}</a> \n", CurrUrl, IdentifIEr, j));
97 }
98
99 if (pagecount - CurrentPage > 10)
100 {
101 Html.AppendFormat("\t<a href=\"{0}{1}{2}\" title=\"{3}\">....</a>\n",
102 CurrUrl,
103 IdentifIEr,
104 (CurrentPage + 10),
105 VIEwType == 0 || VIEwType == 3 ? "下10頁" : "Next Ten Pages"
106 );
107 }
108
109 if (pagecount > CurrentPage)
110 {
111 Html.AppendFormat("\t<a href=\"{0}{1}{2}\" title=\"{3}\">></a> \n\t<a href=\"{0}{1}{4}\" title=\"{5}\">>></a> \n",
112 CurrUrl,
113 IdentifIEr,
114 (CurrentPage + 1),
115 VIEwType == 0 || VIEwType == 3 ? "下一頁" : "Next",
116 pagecount,
117 VIEwType == 0 || VIEwType == 3 ? "末頁" : "Last"
118 );
119 }
120
121 String tx = Template.Replace("{RecordCount}", RecordCount.ToString()).Replace("{PageSize}", PageSize.ToString()).Replace("{PageCount}", pagecount.ToString()).Replace("{CurrentPage}", CurrentPage.ToString()).Replace("{List}", Html.ToString());
122
123 return MvcHtmlString.Create(tx);
124 }


 

此分頁控件支持URL附加參數方式,比如說  xx?page=1 這種方式,

亦能實現自定義的URL得寫方式,比如說 news-list-1(分頁碼).Html

支付中英文兩種界面顯示方式,

對應CSS文件為:

1 .Pagination a{display:block;padding:1px 6px; border:1px solid #14A7FF;float:left;margin-left:2px;color:#14A7FF;font:normal 12px 'Arial'}
2 .Pagination a:hover{background:#14A7FF;color:#fff;border:1px solid #14A7FF;}
3 .Pagination a.curr{color:#CB05F5;background:#fff;border:1px solid #CB05F5;}
4 .Pagination a.curr:hover{color:#CB05F5;background:#fff;border:1px solid #CB05F5;}
5 .Pagination a.noboder{display:block;padding:1px 6px;float:left;margin-left:2px;border:0;}
6 .Pagination a.noboder:hover{display:block;padding:1px 6px;float:left;margin-left:2px;border:0;background:none;}
7 .Pagination span{float:left;margin:3px 6px 0 0;display:block;}

調用方式為:

1 <div class="Pagination">@Html.Pagination((int)ViewBag.PageSize, (int)ViewBag.Index, (int)ViewBag.RecordCount, new { @ViewType = 0, @IdentifIEr = "page", @Encrypt = false })</div>

具體參數為:

/*
* <span>共:{RecordCount}條 {PageSize}條/頁 {CurrentPage}頁/{PageCount}頁</span> {List}
* <span>Page {CurrentPage} of {PageCount} ({RecordCount} items) PageSize:{PageSize}</span> {List}
* @VIEwType = 0, 0中文 1英文 2自定義中文 3自定義英文
* @Template = "<span>共:{RecordCount}條 {PageSize}條/頁 {CurrentPage}頁/{PageCount}頁</span> {List}",
* @Postfix = "",
* @VirtualPath = "",
* @IdentifIEr = "page",
* @Encrypt = false }
*/
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved