zoukankan      html  css  js  c++  java
  • 一个分页的HtmlHelper

     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 }

    ************Code虽易,Debug不易,且码且珍惜************


    本博客仅用于学习记录之用,如有侵权,请Email:tianqy@live.com

  • 相关阅读:
    python-web自动化-Js-滚动条操作
    python-web自动化-键盘操作
    python-web自动化:下拉列表操作
    python-web自动化-鼠标操作
    FastAPI 项目结构组织,工厂模式创建
    JavaScript fetch简单封装
    Python循环删除中遇到的小坑
    20200707 千锤百炼软工人第二天
    20200706 千锤百炼软工人第一天
    小谢第25问:iframe怎么向页面传值?
  • 原文地址:https://www.cnblogs.com/tianqy/p/4007498.html
Copyright © 2011-2022 走看看