欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

.net mvc 分页

程序员文章站 2022-04-28 13:07:39
1 public static MvcHtmlString PageControl(this HtmlHelper html, PageData page) 2 { 3 if (page == null || page.totalpage == 0) 4 return MvcHtmlString.C... ......
 1         public static mvchtmlstring pagecontrol(this htmlhelper html, pagedata page)
 2         {
 3             if (page == null || page.totalpage == 0)
 4                 return mvchtmlstring.create(string.empty);
 5 
 6             page.pageindex = page.pageindex < 1 ? 1 : page.pageindex;
 7             string url = httpcontext.current.request.url.tostring();
 8             if (url.indexof('#') > -1)
 9                 url = url.substring(0, url.indexof('#'));
10 
11             stringbuilder sb = new stringbuilder();
12             sb.append("<div class='paging-toolbar'>");
13             sb.append("<ul class='pagination'>");
14             #region 首页
15             if (page.totalpage > 1)
16                 sb.append("<li><a href='" + geturl(url, 1) + "'>首页</a></li>");
17             #endregion
18 
19             #region 上一页
20             if (page.pageindex > 1)
21                 sb.append("<li><a href='" + geturl(url, page.pageindex - 1) + "'>上一页</a></li>");
22             #endregion
23 
24             #region 中间和当前页
25             var half = page.count % 2 == 0 ? page.count / 2 : page.count / 2 + 1; // 展示页一半
26             var leng = page.count;
27             if (page.count > page.totalpage) leng = page.totalpage;
28 
29             if (page.pageindex > page.totalpage - leng + half)
30             {
31                 for (int i = page.totalpage - leng + 1; i <= page.totalpage; i++)
32                 {
33                     if (i == page.pageindex)
34                     {
35                         sb.append("<li class='active'><a href='" + geturl(url, page.pageindex) + "'>" + page.pageindex + "</a></li>");
36                     }
37                     else
38                     {
39                         sb.append("<li><a href='" + geturl(url, i) + "'>" + i + "</a></li>");
40                     }
41                 }
42             }
43             else
44             {
45                 for (int i = 1; i <= leng; i++)
46                 {
47                     if (i == half)
48                     {
49                         sb.append("<li class='active'><a href='" + geturl(url, page.pageindex) + "'>" + page.pageindex + "</a></li>");
50                     }
51                     else
52                     {
53                         var p = page.pageindex - half + i;
54                         if (p > 0)
55                             sb.append("<li><a href='" + geturl(url, p) + "'>" + p + "</a></li>");
56                         else
57                             leng += 1;
58                     }
59                 }
60             }
64             #endregion
65 
66             #region 下一页
67             if (page.pageindex < page.totalpage)
68                 sb.append("<li><a href='" + geturl(url, page.pageindex < 2 ? 2 : page.pageindex + 1) + "'>下一页</a></li>");
69             #endregion
70 
71             #region 末页
72             if (page.totalpage > 1)
73                 sb.append("<li><a href='" + geturl(url, page.totalpage) + "'>末页</a></li>");
74             #endregion
75 
76             sb.append("</ul>");
77             sb.append("</div>");
78 
79             return mvchtmlstring.create(sb.tostring());
80         }
 1     public class pagedata
 2     {
 3         /// <summary>
 4         /// 展示的页数
 5         /// </summary>
 6         public int count { get; set; }
 7 
 8         /// <summary>
 9         /// 当前页
10         /// </summary>
11         public int pageindex { get; set; }
12 
13         /// <summary>
14         /// 总页数
15         /// </summary>
16         public int totalpage { get; set; }
17 
18         /// <summary>
19         /// 总条数
20         /// </summary>
21         public int totalrecord { get; set; }
22     }