ASP.NET MVC 3.0 分页控件

看到一些哥们在写MVC的分页控件,我也来凑个热闹,新手上路,请多多包涵,提出中肯意见,不要一味的拍砖,打击士气,以后还会有很多其它控件发布.

效果如下

代码如下

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 }
*/

本站具备完整的演示DEMO,下载地址为: 点击我下载

此为第一个MVC 3控件,请多多包涵,

原创作品,转载请注明出处,转载者不得以本示例去CSDN上骗下载分

posted on 2011-04-24 03:06  柴哥  阅读(3988)  评论(18编辑  收藏  举报

© Devin!NT Skyline Co.,ltd