看到一些哥们在写MVC的分页控件,我也来凑个热闹,新手上路,请多多包涵,提出中肯意见,不要一味的拍砖,打击士气,以后还会有很多其它控件发布.
效果如下
代码如下
1 ///
2 /// 分页控件 3 /// 4 ///
5 /// 单页条数
6 /// 当前页
7 /// 总记录数
8 /// 扩展参数
9 ///
10 public static MvcHtmlString Pagination(this HtmlHelper helper, Int32 PageSize, Int32 CurrentPage, Int32 RecordCount, Object Attribute) 11 { 12 /* 13 * 共:{RecordCount}条 {PageSize}条/页 {CurrentPage}页/{PageCount}页 {List} 14 * Page {CurrentPage} of {PageCount} ({RecordCount} items) PageSize:{PageSize} {List} 15 * @ViewType = 0, 0中文 1英文 2自定义中文 3自定义英文 16 * @Template = "共:{RecordCount}条 {PageSize}条/页 {CurrentPage}页/{PageCount}页 {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共:{RecordCount}条 {PageSize}条/页 {CurrentPage}页/{PageCount}页 {List}" : 31 "\tPage {CurrentPage} of {PageCount} ({RecordCount} items) PageSize:{PageSize} {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
{0} \n", j) : String.Format("\t{2} \n", CurrUrl, Identifier, j)); 97 } 98 99 if (pagecount - CurrentPage > 10)
100 {
101 html.AppendFormat("\t....\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> \n\t>> \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
具体参数为:
/* * 共:{RecordCount}条 {PageSize}条/页 {CurrentPage}页/{PageCount}页 {List} * Page {CurrentPage} of {PageCount} ({RecordCount} items) PageSize:{PageSize} {List} * @ViewType = 0, 0中文 1英文 2自定义中文 3自定义英文 * @Template = "共:{RecordCount}条 {PageSize}条/页 {CurrentPage}页/{PageCount}页 {List}", * @Postfix = "", * @VirtualPath = "", * @Identifier = "page", * @Encrypt = false }
*/
本站具备完整的演示DEMO,下载地址为: 点击我下载
此为第一个MVC 3控件,请多多包涵,
原创作品,转载请注明出处,转载者不得以本示例去CSDN上骗下载分
延伸阅读
最新评论