首页 > 编程语言 >c# MVC分页控件帮助类

c# MVC分页控件帮助类

时间:2023-09-25 10:34:01浏览次数:34  
标签:控件 extendPage endPage c# int MVC curPage countPage Append

 

c# 使用mvc 分页控件

   /// <summary>
   /// 获得伪静态页码显示链接
   /// </summary>
   /// <param name="curPage">当前页数</param>
   /// <param name="countPage">总页数</param>
   /// <param name="url">超级链接地址</param>
   /// <param name="extendPage">周边页码显示个数上限</param>
   /// <returns>页码html</returns>
   public static string GetStaticPageNumbers(int curPage, int countPage, string url, int extendPage)
   {
       int startPage = 1;
       int endPage = 1;

       string t1 = "<a title=\"首页\" href=\"" + string.Format(url, "1") + "\">首页</a><a title=\"上一页\" href=\"" + string.Format(url, (curPage - 1)) + "\">上一页</a>";
       string t2 = "<a title=\"下一页\" href=\"" + string.Format(url, (curPage + 1)) + "\">下一页</a><a title=\"尾页\" href=\"" + string.Format(url, countPage) + "\">尾页</a>";

       if (countPage < 1) countPage = 1;
       if (extendPage < 3) extendPage = 2;

       if (countPage > extendPage)
       {
           if (curPage - (extendPage / 2) > 0)
           {
               if (curPage + (extendPage / 2) < countPage)
               {
                   startPage = curPage - (extendPage / 2);
                   endPage = startPage + extendPage - 1;
               }
               else
               {
                   endPage = countPage;
                   startPage = endPage - extendPage + 1;
                   t2 = "<a title=\"下一页\" class=\"disabled next\" href=\"javascript:;\">下一页</a><a class=\"disabled end\" title=\"尾页\" href=\"javascript:;\">尾页</a>";
               }
           }
           else
           {
               endPage = extendPage;
               t1 = "<a title=\"首页\" class=\"disabled begin\" href=\"javascript:;\">首页</a><a class=\"disabled previou\" title=\"上一页\" href=\"javascript:;\">上一页</a>";
           }
       }
       else
       {
           startPage = 1;
           endPage = countPage;
           t1 = "<a title=\"首页\" class=\"disabled begin\" href=\"javascript:;\">首页</a><a class=\"disabled previou\" title=\"上一页\" href=\"javascript:;\">上一页</a>";
           t2 = "<a title=\"下一页\" class=\"disabled next\" href=\"javascript:;\">下一页</a><a class=\"disabled end\" title=\"尾页\" href=\"javascript:;\">尾页</a>";
       }

       if (curPage == 1 && curPage != endPage)
       {
           t2 = "<a title=\"下一页\" class=\"next\" href=\"" + string.Format(url, (curPage + 1)) + "\">下一页</a><a class=\"end\" title=\"尾页\" href=\"" + string.Format(url, countPage) + "\">尾页</a>";
       }
       else if (curPage < endPage && curPage != 1)
       {
           t2 = "<a title=\"下一页\" class=\"next\" href=\"" + string.Format(url, (curPage + 1)) + "\">下一页</a><a class=\"end\" title=\"尾页\" href=\"" + string.Format(url, countPage) + "\">尾页</a>";
       }

       if (curPage > 1 && curPage != endPage)
       {
           t1 = "<a title=\"首页\" class=\"begin\" href=\"" + string.Format(url, "1") + "\">首页</a><a class=\"previou\" title=\"上一页\" href=\"" + string.Format(url, (curPage - 1)) + "\">上一页</a>";
       }
       else if (curPage == endPage && curPage != 1)
       {
           t1 = "<a title=\"首页\" class=\"begin\" href=\"" + string.Format(url, "1") + "\">首页</a><a class=\"previou\" title=\"上一页\" href=\"" + string.Format(url, (curPage - 1)) + "\">上一页</a>";
       }


       StringBuilder s = new StringBuilder("");

       s.Append(t1);
       for (int i = startPage; i <= endPage; i++)
       {
           if (i == curPage)
           {
               s.Append("<a href=\"javascript:;\" class=\"active\" title=\"第 " + i + " 页\">");
               s.Append(i);
               s.Append("</a>");
           }
           else
           {
               s.Append("<a title=\"第 " + i + " 页\" href=\"");
               s.Append(string.Format(url, i));
               s.Append("\">");
               s.Append(i);
               s.Append("</a>");
           }
       }
       s.Append(t2);

       return s.ToString();
   }

   /// <summary>
   /// 获得AJAX页码显示链接
   /// </summary>
   /// <param name="curPage">当前页数</param>
   /// <param name="countPage">总页数</param>
   /// <param name="url">超级链接地址</param>
   /// <param name="extendPage">周边页码显示个数上限</param>
   /// <returns>页码html</returns>
   public static string GetAjaxPageNumbers(int curPage, int countPage, string url, int extendPage)
   {
       int startPage = 1;
       int endPage = 1;

       string t1 = "<a title=\"首页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", "1") + "\">首页</a><a title=\"上一页\"  href=\"javascript:;\" onclick=\"" + url.Replace("{0}", (curPage - 1).ToString()) + "\">上一页</a>";
       string t2 = "<a title=\"下一页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", (curPage + 1).ToString()) + "\">下一页</a><a title=\"尾页\"  href=\"javascript:;\" onclick=\"" + url.Replace("{0}", countPage.ToString()) + "\">尾页</a>";

       string t3 = "";
       string t4 = "";

       if (countPage < 1) countPage = 1;
       if (extendPage < 3) extendPage = 2;

       if (countPage > extendPage)
       {
           if (curPage - (extendPage / 2) > 0)
           {
               if (curPage + (extendPage / 2) < countPage)
               {
                   startPage = curPage - (extendPage / 2);
                   endPage = startPage + extendPage - 1;
               }
               else
               {
                   endPage = countPage;
                   startPage = endPage - extendPage + 1;

                   if (curPage >= countPage)
                       t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>";
               }
           }
           else
           {
               endPage = extendPage;

               if (curPage <= 1 || countPage == 1)
                   t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>";
           }
       }
       else
       {
           startPage = 1;
           endPage = countPage;

           if (curPage <= 1 || countPage == 1)
               t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>";

           if (curPage >= countPage)
               t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>";
       }

       StringBuilder s = new StringBuilder("");

       s.Append(t1);

       if (curPage > (extendPage / 2) + 1)
           t3 = "<a title=\"上翻\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", startPage.ToString()) + "\">...</a>";

       s.Append(t3);

       for (int i = startPage; i <= endPage; i++)
       {
           if (i == curPage)
           {
               s.Append("<a class=\"active\" href=\"javascript:;\" title=\"第 " + i + " 页\"> ");
               s.Append(i);
               s.Append("</a>");
           }
           else
           {
               s.Append("<a title=\"第 " + i + " 页\" href=\"javascript:;\"");
               s.Append(" onclick=\"");
               s.Append(url.Replace("{0}", i.ToString()));
               s.Append("\">");
               s.Append(i);
               s.Append("</a>");
           }
       }

       if (curPage + (extendPage / 2) < countPage)
           t4 = "<a title=\"下翻\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", endPage.ToString()) + "\">...</a>";

       s.Append(t4);

       s.Append(t2);

       return s.ToString();
   }

   /// <summary>
   /// 获得AJAX页码显示链接
   /// </summary>
   /// <param name="curPage">当前页数</param>
   /// <param name="countPage">总页数</param>
   /// <param name="url">超级链接地址</param>
   /// <param name="extendPage">周边页码显示个数上限</param>
   /// <returns>页码html</returns>
   public static string GetAjaxPageNumbers(int curPage, int countPage, string url, int extendPage, bool isInstitutional = false)
   {
       int startPage = 1;
       int endPage = 1;

       //string t1 = "<a title=\"首页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", "1") + "\">首页</a><a title=\"上一页\"  href=\"javascript:;\" onclick=\"" + url.Replace("{0}", (curPage - 1).ToString()) + "\">上一页</a>";
       string t1 = "<a href=\"javascript:;\" class=\"layui-laypage-prev\" data-page=\"" + (curPage - 1) + "\" onclick=\"" + url.Replace("{0}", (curPage - 1).ToString()) + "\">上一页</a>";
       string t2 = "<a href=\"javascript:;\" class=\"layui-laypage-next\" data-page=\""+ (curPage + 1) + "\" onclick=\"" + url.Replace("{0}", (curPage + 1).ToString()) + "\">下一页</a>";
       //string t2 = "<a title=\"下一页\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", (curPage + 1).ToString()) + "\">下一页</a><a title=\"尾页\"  href=\"javascript:;\" onclick=\"" + url.Replace("{0}", countPage.ToString()) + "\">尾页</a>";

       string t3 = "";
       string t4 = "";

       if (countPage < 1) countPage = 1;
       if (extendPage < 3) extendPage = 2;

       if (countPage > extendPage)
       {
           if (curPage - (extendPage / 2) > 0)
           {
               if (curPage + (extendPage / 2) < countPage)
               {
                   startPage = curPage - (extendPage / 2);
                   endPage = startPage + extendPage - 1;
               }
               else
               {
                   endPage = countPage;
                   startPage = endPage - extendPage + 1;

                   if (curPage >= countPage)
                       t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>";
               }
           }
           else
           {
               endPage = extendPage;

               if (curPage <= 1 || countPage == 1)
                   //t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>";
               t1 = "<a href=\"javascript:;\" class=\"layui-laypage-prev layui-disabled\" data-page=\"0\">上一页</a>";
           }
       }
       else
       {
           startPage = 1;
           endPage = countPage;

           if (curPage <= 1 || countPage == 1)
               t1 = "<a class=\"disabled layui-disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled layui-disabled\" title =\"上一页\" href=\"javascript:;\">上一页</a>";

           if (curPage >= countPage)
               t2 = "<a class=\"disabled layui-disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled layui-disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>";
       }

       StringBuilder s = new StringBuilder("");
       var sdev = "<div class=\"flex-end\" id=\"paging\"><div class=\"layui-box layui-laypage layui-laypage-default\" id=\"layui-laypage-1\">";
       if (isInstitutional)
       {
           s.Append(sdev);
       }

       s.Append(t1);

       if (curPage > (extendPage / 2) + 1)
           t3 = "<a title=\"上翻\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", startPage.ToString()) + "\">...</a>";

       s.Append(t3);

       for (int i = startPage; i <= endPage; i++)
       {
           if (i == curPage)
           {
               //s.Append("<a class=\"active\" href=\"javascript:;\" title=\"第 " + i + " 页\"> ");
               //s.Append(i);
               //s.Append("</a>");
               s.Append("<span class=\"layui-laypage-curr\"><em class=\"layui-laypage-em\"></em><em>" + i + "</em></span>");
           }
           else
           {
               s.Append("<a title=\"第 " + i + " 页\" href=\"javascript:;\"");
               s.Append(" onclick=\"");
               s.Append(url.Replace("{0}", i.ToString()));
               s.Append("\">");
               s.Append(i);
               s.Append("</a>");
               //s.Append("<span class=\"layui-laypage-curr\"><em class=\"layui-laypage-em\"></em><em>" + i + "</em></span>");
           }
       }

       if (curPage + (extendPage / 2) < countPage)
           t4 = "<a title=\"下翻\" href=\"javascript:;\" onclick=\"" + url.Replace("{0}", endPage.ToString()) + "\">...</a>";

       s.Append(t4);

       s.Append(t2);
       var sdev1 = "</div></div>";
       if (isInstitutional)
       {
           s.Append(sdev1);
       }

       return s.ToString();
   }

   /// <summary>
   /// 获得页码显示链接
   /// </summary>
   /// <param name="curPage">当前页数</param>
   /// <param name="countPage">总页数</param>
   /// <param name="url">超级链接地址</param>
   /// <param name="extendPage">周边页码显示个数上限</param>
   /// <returns>页码html</returns>
   public static string GetPageNumbers(int curPage, int countPage, string url, int extendPage)
   {
       int startPage = 1;
       int endPage = 1;

       if (url.IndexOf("?") == -1)
       {
           url = url + "?";
       }
       else
       {
           url = url + "&";
       }

       string t1 = "<a title=\"首页\" href=\"" + url + "page=1" + "\">首页</a><a title=\"上一页\" href=\"" + url + "page=" + (curPage - 1) + "\">上一页</a>";
       string t2 = "<a title=\"下一页\" href=\"" + url + "page=" + (curPage + 1) + "\">下一页</a><a title=\"尾页\" href=\"" + url + "page=" + countPage + "\">尾页</a>";

       string t3 = "";
       string t4 = "";

       if (countPage < 1) countPage = 1;
       if (extendPage < 3) extendPage = 2;

       if (countPage > extendPage)
       {
           if (curPage - (extendPage / 2) > 0)
           {
               if (curPage + (extendPage / 2) < countPage)
               {
                   startPage = curPage - (extendPage / 2);
                   endPage = startPage + extendPage - 1;
               }
               else
               {
                   endPage = countPage;
                   startPage = endPage - extendPage + 1;

                   if (curPage >= countPage)
                       t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>";
               }
           }
           else
           {
               endPage = extendPage;

               if(curPage <= 1 || countPage == 1)
                   t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>";
           }
       }
       else
       {
           startPage = 1;
           endPage = countPage;

           if (curPage <= 1 || countPage == 1)
               t1 = "<a class=\"disabled\" title=\"首页\" href=\"javascript:;" + "\">首页</a><a class=\"disabled\" title=\"上一页\" href=\"javascript:;\">上一页</a>";

           if (curPage >= countPage)
               t2 = "<a class=\"disabled\" title=\"下一页\" href=\"javascript:;\">下一页</a><a class=\"disabled\" title=\"尾页\" href=\"javascript:;\">尾页</a>";
       }

       StringBuilder s = new StringBuilder("");

       s.Append(t1);

       if (curPage > (extendPage / 2) + 1)
           t3 = "<a title=\"上翻\" href=\"" + url + "page=" + startPage + "\">...</a>";

       s.Append(t3);

       for (int i = startPage; i <= endPage; i++)
       {
           if (i == curPage)
           {
               s.Append("<a class=\"active\" href=\"javascript:;\" title=\"第 " + i + " 页\"> ");
               s.Append(i);
               s.Append("</a>");
           }
           else
           {
               s.Append("<a title=\"第 " + i + " 页\" href=\"");
               s.Append(url);
               s.Append("page=");
               s.Append(i);
               s.Append("\">");
               s.Append(i);
               s.Append("</a>");
           }
       }

       if (curPage + (extendPage / 2) < countPage)
           t4= "<a title=\"下翻\" href=\"" + url + "page=" + endPage + "\">...</a>";

       s.Append(t4);

       s.Append(t2);

       return s.ToString();
   }

   /// <summary>
   /// blog 分页
   /// </summary>
   /// <param name="curPage"></param>
   /// <param name="countPage"></param>
   /// <param name="url"></param>
   /// <param name="extendPage"></param>
   /// <returns></returns>
   public static string GetBlogPageNumbers(int curPage, int countPage, string url, int extendPage)
   {
       int startPage = 1;
       int endPage = 1;

       string t1 = "<a class=\"begin\" title=\"首页\" href=\"" + url.Replace("{page}", "1") + "\"><</a> ";
       string t2 = "<a class=\"end\" title=\"尾页\" href=\"" + url.Replace("{page}", countPage.ToString()) + "\">></a>";

       if (countPage < 1) countPage = 1;
       if (extendPage < 3) extendPage = 2;

       if (countPage > extendPage)
       {
           if (curPage - (extendPage / 2) > 0)
           {
               if (curPage + (extendPage / 2) < countPage)
               {
                   startPage = curPage - (extendPage / 2);
                   endPage = startPage + extendPage - 1;
               }
               else
               {
                   endPage = countPage;
                   startPage = endPage - extendPage + 1;
                   t2 = "";
               }
           }
           else
           {
               endPage = extendPage;
               t1 = "";
           }
       }
       else
       {
           startPage = 1;
           endPage = countPage;
           t1 = "";
           t2 = "";
       }

       StringBuilder s = new StringBuilder("");

       s.Append(t1);
       for (int i = startPage; i <= endPage; i++)
       {
           if (i == curPage)
           {
               s.Append("<a title=\"第 " + i + " 页\" href=\"javascript:;\" class=\"current\">");
               s.Append(i);
               s.Append("</a> ");
           }
           else
           {
               s.Append("<a title=\"第 " + i + " 页\" href=\"");
               s.Append(url.Replace("{page}", i.ToString()));
               s.Append("\">");
               s.Append(i);
               s.Append("</a> ");
           }
       }
       s.Append(t2);

       return s.ToString();
   }
  

使用

GetPageNumbers((Int32)Model.CurrentPage, (Int32)Model.TotalPages, "**/**?state=" + GetRequestInt("state", -1)+"......." )

后面的省略号可以拼接添加多个参数

标签:控件,extendPage,endPage,c#,int,MVC,curPage,countPage,Append
From: https://www.cnblogs.com/wuyabaibsd/p/17727237.html

相关文章

  • Vs2022 开发Grpc
    使用VS2022开发遇到了不少坑,记录一下VS貌似没有类似WebService添加接口的模板,需要手动添加.proto文件,而且.proto文件,也没有对应模板,需要添加一个文本文件,改成需要的名字添加完.proto文件,需要用Grpc.Tools编译生成对应所需的文件,使用dotnetaddpackageGrpc.Tools这里我遇......
  • 2023年东莞/深圳CPDA数据分析师认证报名小哥哥来这
    CPDA数据分析师认证是大数据方面的认证,助力数据分析人员打下扎实的数据分析基础知识功底,为入门数据分析保驾护航。帮助数据分析人员掌握系统化的数据分析思维和方法论,提升工作效率和决策能力,遇到问题能够举一反三,为大部分决策难题提供解决方案。帮助数据分析人员掌握几种通用的数据......
  • Easysearch 压缩功能的显著提升:从 8.7GB 到 1.4GB
    引言在海量数据的存储和处理中,索引膨胀率是一个不可忽视的关键指标。它直接影响了存储成本和查询性能。近期,Easysearch在这方面取得了显著的进展,其压缩功能的效果远超过了之前的版本。本文将详细介绍这一进展。Easysearch各版本压缩性能对比根据之前文章的数据,Easysearchv1.1在......
  • 解决 undefined function bcdiv()问题
    在Deepin中php7.2遇到问题:UncaughtError:Calltoundefinedfunctionbcdiv()1bcdiv函数的作用(点我查看)原因是因为缺少了PHP的bcmath扩展,导致电脑无法识别该函数。解决办法:1、查看当前php版本PHP-v12、更新源Centos下:sudoyumupdate1Ubuntu或Deepin下:sudoapt-get......
  • springsecurity 6.x
    springboot+springsecurity6.x的配置securityConfig.java登录页面的DIY@BeanpublicSecurityFilterChainsecurityFilterChain(HttpSecurityhttp)throwsException{http.authorizeHttpRequests(author->author.requestMatchers("/......
  • UE4创建C++版项目遇到的错误
    1.VisualStudio2017version15.6isrequiredtocompileonWindows.PleaseinstallupdatesthroughtheVisualStudioinstaller.说是VS201715.0.0版本太低,需要VS201715.6才可以。但是又不想更新。。所以直接注释掉这句断言代码。就不报错了该文件名字叫“WindowsPl......
  • Jenkins 命令执行 -- jetty 敏感信息泄露 --(CVE-2021-2816)&&(CVE-2017-1000353)&&(C
    Jenkins命令执行--jetty敏感信息泄露--(CVE-2021-2816)&&(CVE-2017-1000353)&&(CVE-2018-1000861)jetty敏感信息泄露(CVE-2021-28169)漏洞简介对于<=9.4.40、<=10.0.2、<=11.0.2的EclipseJetty版本,对带有双重编码路径的ConcatServlet的请求可以访问WEB-INF目录......
  • CF1801D The way home
    原题翻译非常好的一个题,有两种做法方法1:flody+dp首先我们确定一个最优行走方案:从\(1\)号节点赚到足够钱后通过最短路到达\(x_1\),在\(x_1\)赚够足够钱后到达\(x_2\),在\(x_2\)赚够足够钱后到达\(x_3\),如此往复后到达终点现在我们有一个问题:从\(u\)到\(v\)的路......
  • rac多路径下添加lun(centos 6)
    环境:OS:Centos6.9DB:11.2.0.4 1.虚拟机添加磁盘 2.每个登录查看磁盘情况节点1:[root@rac01bin]#lsblkNAMEMAJ:MINRMSIZEROTYPEMOUNTPOINTsr011:011024M0romsda8:0......
  • Mac实现IDEA和nginx前后端联调(新手向)
    前言今天看黑马程序员的javaweb课程,看到前后端联调这一节犯了难。因为老师使用的开发环境是win10,而我用的是Mac,不想装虚拟机,就想着研究一下如何在Mac上实现前后端联调。因为之前只简单学了一下前端三大件,没有系统学过nginx,所以有什么问题请多包涵。前提:获取老师的文件包我们需要......