码迷,mamicode.com
首页 > Web开发 > 详细

一个分页的HtmlHelper

时间:2014-10-05 23:46:09      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   ar   for   sp   div   

 1 namespace System.Web.Mvc
 2 {
 3     public static class TianqyHtmlHelper
 4     {
 5         /// <summary>
 6         /// 分页HtmlHelper
 7         /// </summary>
 8         public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
 9         {
10             pageSize = pageSize == 0 ? 3 : pageSize;
11             var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
12             var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
13             var outPut = new StringBuilder();
14             if (totalPages > 1)
15             {
16                 //首页
17                 if (currentPage != 1)
18                 {
19                     outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex=1&pageSize={1}‘>首页</a>", redirectTo, pageSize);
20                 }
21                 outPut.Append(" ");
22                 //上一页
23                 if (currentPage > 1)
24                 {
25                     outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>上一页</a>", redirectTo, currentPage - 1, pageSize);
26                 }
27                 outPut.Append(" ");
28                 //一共最多显示10个页码,前5个,后5个
29                 int n = 5;
30                 for (int i = 0; i <= 10; i++)
31                 {
32                     if ((currentPage + i - n) > 1 && (currentPage + i - n) <= totalPages)
33                     {
34                         if (i == n)
35                         {
36                             outPut.AppendFormat("<a class=‘pageCurrent‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a>", redirectTo, currentPage, pageSize, currentPage);
37                         }
38                         else
39                         {
40                             outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>{3}</a>", redirectTo, currentPage + i - n, pageSize, currentPage + i - n);
41                         }
42                     }
43                     outPut.Append(" ");
44                 }
45                 //下一页
46                 if (currentPage < totalPages)
47                 {
48                     outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>下一页</a>", redirectTo, currentPage + 1, pageSize);
49                 }
50                 outPut.Append(" ");
51                 //末页
52                 if (currentPage != totalPages)
53                 {
54                     outPut.AppendFormat("<a class=‘pageLink‘ href=‘{0}?pageIndex={1}&pageSize={2}‘>末页</a>", redirectTo, totalPages, pageSize);
55                 }
56                 outPut.Append(" ");
57             }
58             outPut.AppendFormat("第{0}页/共{1}页", currentPage, totalPages);
59             return new HtmlString(outPut.ToString());
60         }
61     }
62 }

 

一个分页的HtmlHelper

标签:style   blog   http   color   os   ar   for   sp   div   

原文地址:http://www.cnblogs.com/tianqy/p/4007498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!