1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using System.Web.Routing;
7 using System.Text;
8 using System.Web.Mvc.Html;
9 namespace application.Helpers
10 {
11 public static class PagerExtensions
12 {
13 /*helper分页
14 *使用方式:<%@ Import Namespace="ExpoShiep.Helpers" %>
15 *<%=Html.Pager("page",10,100)%>*/
16 /// < summary>
17 /// 分页Pager显示
18 /// < /summary>
19 /// < param name="html">< /param>
20 /// < param name="currentPageStr">标识当前页码的QueryStringKey< /param>
21 /// < param name="pageSize">每页显示< /param>
22 /// < param name="totalCount">总数据量< /param>
23 /// < returns>< /returns>
24 public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
25 {
26 var queryString = html.ViewContext.HttpContext.Request.QueryString;
27 int currentPage = 1; //当前页
28 var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
29 var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
30 var output = new System.Text.StringBuilder();
31 if (!string.IsNullOrEmpty(queryString[currentPageStr]))
32 {//与相应的QueryString绑定
33 foreach (string key in queryString.Keys)
34 if (queryString[key] != null && !string.IsNullOrEmpty(key))
35 dict[key] = queryString[key];
36 int.TryParse(queryString[currentPageStr], out currentPage);
37 }
38 else
39 {//获取 ~/Page/{page number} 的页号参数
40 int.TryParse(dict[currentPageStr].ToString(), out currentPage);
41 }
42 if (currentPage <= 0)
43 currentPage = 1;
44 if (totalPages > 1)
45 {
46 if (currentPage != 1)
47 {//处理首页连接
48 dict[currentPageStr] = 1;
49 output.AppendFormat("{0} ", html.RouteLink("首页", dict));
50 }
51 if (currentPage > 1)
52 {//处理上一页的连接
53 dict[currentPageStr] = currentPage - 1;
54 output.Append(html.RouteLink("上一页", dict));
55 }
56 else
57 {
58 //output.Append("上一页");
59 dict[currentPageStr] = currentPage;
60 output.Append(html.RouteLink("上一页", dict));
61 }
62 output.Append(" ");
63 int currint = 5;
64 for (int i = 0; i <= 10; i++)
65 {//一共最多显示10个页码,前面5个,后面5个
66 if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
67 if (currint == i)
68 {//当前页处理
69 //output.Append(string.Format("[{0}]", currentPage));
70 dict[currentPageStr] = currentPage;
71 output.AppendFormat("<span class=\"current\">{0}</span>", currentPage);
72 }
73 else
74 {
75 //一般页处理
76 dict[currentPageStr] = currentPage + i - currint;
77 output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
78 }
79 output.Append(" ");
80 }
81 if (currentPage < totalPages)
82 {
83 //处理下一页的链接
84 dict[currentPageStr] = currentPage + 1;
85 output.Append(html.RouteLink("下一页", dict));
86 }
87 else
88 {
89 //output.Append("下一页");
90 dict[currentPageStr] = currentPage;
91 output.Append(html.RouteLink("下一页", dict));
92 }
93 output.Append(" ");
94 if (currentPage != totalPages)
95 {
96 dict[currentPageStr] = totalPages;
97 output.Append(html.RouteLink("末页", dict));
98 }
99 output.Append(" ");
100 }
101 output.AppendFormat("{0} / {1}", currentPage, totalPages);//这个统计加不加都行
102 return output.ToString();
103 }
104 }
105 }
复制代码
CSS:
1 .pager
2 {
3 font-size: 12px;
4 margin: 8px 0;
5 padding: 3px 0 3px;
6 text-align: left;
7 }
8
9 .pager .current
10 {
11 background-color: #06c;
12 border: 1px solid #009;
13 color: #fff;
14 font-weight: bold;
15 margin-top: 4px;
16 padding: 3px 5px;
17 text-align: center;
18 }
19
20 .pager a
21 {
22 margin: 4px 3px;
23 border: 1px solid #9AAFE5;
24 padding: 3px 5px;
25 text-align: center;
26 text-decoration: none;
27 color: #2E6AB1;
28 }
29
30 .pager .pagerInput
31 {
32 padding: 3px 0 0 0;
33 border: 1px solid #9AAFE5;
34 text-align: center;
35 text-decoration: none;
36 height: 16px;
37 width: 30px;
38 }
39 .pager .pagerButton
40 {
41 border: 1px solid #9AAFE5;
42 cursor: pointer;
43 }
44
复制代码
View中使用:
1 <div class="pager">
2 <%= Html.Pager("page", 10,300))%>
3 </div>
复制代码
哈哈,这个博客的处女文啦~~
首先声明,这个分页代码并不是出自我手哈,借用了网上的一段代码,然后加了个css,变的好看一些啦~~
原作者忘记是谁了,实在找不到了……万分抱歉啊~~
效果如下:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Web;
5 using System.Web.Mvc;
6 using System.Web.Routing;
7 using System.Text;
8 using System.Web.Mvc.Html;
9 namespace application.Helpers
10 {
11 public static class PagerExtensions
12 {
13 /*helper分页
14 *使用方式:<%@ Import Namespace="ExpoShiep.Helpers" %>
15 *<%=Html.Pager("page",10,100)%>*/
16 /// < summary>
17 /// 分页Pager显示
18 /// < /summary>
19 /// < param name="html">< /param>
20 /// < param name="currentPageStr">标识当前页码的QueryStringKey< /param>
21 /// < param name="pageSize">每页显示< /param>
22 /// < param name="totalCount">总数据量< /param>
23 /// < returns>< /returns>
24 public static string Pager(this HtmlHelper html, string currentPageStr, int pageSize, int totalCount)
25 {
26 var queryString = html.ViewContext.HttpContext.Request.QueryString;
27 int currentPage = 1; //当前页
28 var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
29 var dict = new System.Web.Routing.RouteValueDictionary(html.ViewContext.RouteData.Values);
30 var output = new System.Text.StringBuilder();
31 if (!string.IsNullOrEmpty(queryString[currentPageStr]))
32 {//与相应的QueryString绑定
33 foreach (string key in queryString.Keys)
34 if (queryString[key] != null && !string.IsNullOrEmpty(key))
35 dict[key] = queryString[key];
36 int.TryParse(queryString[currentPageStr], out currentPage);
37 }
38 else
39 {//获取 ~/Page/{page number} 的页号参数
40 int.TryParse(dict[currentPageStr].ToString(), out currentPage);
41 }
42 if (currentPage <= 0)
43 currentPage = 1;
44 if (totalPages > 1)
45 {
46 if (currentPage != 1)
47 {//处理首页连接
48 dict[currentPageStr] = 1;
49 output.AppendFormat("{0} ", html.RouteLink("首页", dict));
50 }
51 if (currentPage > 1)
52 {//处理上一页的连接
53 dict[currentPageStr] = currentPage - 1;
54 output.Append(html.RouteLink("上一页", dict));
55 }
56 else
57 {
58 //output.Append("上一页");
59 dict[currentPageStr] = currentPage;
60 output.Append(html.RouteLink("上一页", dict));
61 }
62 output.Append(" ");
63 int currint = 5;
64 for (int i = 0; i <= 10; i++)
65 {//一共最多显示10个页码,前面5个,后面5个
66 if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
67 if (currint == i)
68 {//当前页处理
69 //output.Append(string.Format("[{0}]", currentPage));
70 dict[currentPageStr] = currentPage;
71 output.AppendFormat("<span class=\"current\">{0}</span>", currentPage);
72 }
73 else
74 {
75 //一般页处理
76 dict[currentPageStr] = currentPage + i - currint;
77 output.Append(html.RouteLink((currentPage + i - currint).ToString(), dict));
78 }
79 output.Append(" ");
80 }
81 if (currentPage < totalPages)
82 {
83 //处理下一页的链接
84 dict[currentPageStr] = currentPage + 1;
85 output.Append(html.RouteLink("下一页", dict));
86 }
87 else
88 {
89 //output.Append("下一页");
90 dict[currentPageStr] = currentPage;
91 output.Append(html.RouteLink("下一页", dict));
92 }
93 output.Append(" ");
94 if (currentPage != totalPages)
95 {
96 dict[currentPageStr] = totalPages;
97 output.Append(html.RouteLink("末页", dict));
98 }
99 output.Append(" ");
100 }
101 output.AppendFormat("{0} / {1}", currentPage, totalPages);//这个统计加不加都行
102 return output.ToString();
103 }
104 }
105 }
复制代码
CSS:
1 .pager
2 {
3 font-size: 12px;
4 margin: 8px 0;
5 padding: 3px 0 3px;
6 text-align: left;
7 }
8
9 .pager .current
10 {
11 background-color: #06c;
12 border: 1px solid #009;
13 color: #fff;
14 font-weight: bold;
15 margin-top: 4px;
16 padding: 3px 5px;
17 text-align: center;
18 }
19
20 .pager a
21 {
22 margin: 4px 3px;
23 border: 1px solid #9AAFE5;
24 padding: 3px 5px;
25 text-align: center;
26 text-decoration: none;
27 color: #2E6AB1;
28 }
29
30 .pager .pagerInput
31 {
32 padding: 3px 0 0 0;
33 border: 1px solid #9AAFE5;
34 text-align: center;
35 text-decoration: none;
36 height: 16px;
37 width: 30px;
38 }
39 .pager .pagerButton
40 {
41 border: 1px solid #9AAFE5;
42 cursor: pointer;
43 }
44
复制代码
View中使用:
1 <div class="pager">
2 <%= Html.Pager("page", 10,300))%>
3 </div>
复制代码