原创

C#分页

一个简单的C#分页小模块

using System;
namespace Page
{
    class Program
    {
        //url连接,countdatas表数据总行数,currentpage第几页(0是第一页),everpage每页多少行,beforlaterln前后长度
        public String pagestr(String url, int countdatas, int currentpage, int everpage, int beforlaterln)
        {
            String page = "";
            int intpart = countdatas / everpage;//整数
            int remainderpart = countdatas % everpage;//余数
            int countpage = intpart;//取整数
            currentpage = currentpage < 0 ? 0 : currentpage;
            if (remainderpart == 0)
            {//余数判断
                currentpage = currentpage > countpage - 1 ? countpage - 1 : currentpage;
                int pp = currentpage + 1;
                String strbe = "";
                int pagebefore = pp - beforlaterln - 1;
                for (int i = pp; i > pagebefore; i--)
                {
                    if (i > 0 && i < pp)
                    {
                        strbe = "<a class='num' href='" + url + "&p=" + (i - 1) + "'>" + i + "</a>" + strbe;
                    }
                }
                if (pp <= 1) { strbe = ""; } else { strbe = "<a class='num' href='" + url + "&p=0'>首页</a>" + "<a class='num' href='" + url + "&p=" + (pp - 2) + "'>上一页</a>" + strbe; }
                String strmid = "<span class='current'>" + pp + "</span>";
                String strlater = "";
                int pagelater = pp + beforlaterln;
                for (int i = pp; i < pagelater; i++)
                {
                    if (i < countpage)
                    {
                        strlater += "<a class='num' href='" + url + "&p=" + (i) + "'>" + (i + 1) + "</a>";
                    }
                }
                if (pp >= countpage) { } else { strlater += "<a class='num' href='" + url + "&p=" + pp + "'>下一页</a>" + "<a class='num' href='" + url + "&p=" + (countpage - 1) + "'>尾页</a>"; }
                page = strbe + strmid + strlater;
            }
            else
            {
                countpage += 1;//总页数
                currentpage = currentpage > countpage - 1 ? countpage - 1 : currentpage;
                int pp = currentpage + 1;
                String strbe = "";
                int pagebefore = pp - beforlaterln - 1;
                for (int i = pp; i > pagebefore; i--)
                {
                    if (i > 0 && i < pp)
                    {
                        strbe = "<a class='num' href='" + url + "&p=" + (i - 1) + "'>" + i + "</a>" + strbe;
                    }
                }
                if (pp <= 1) { strbe = ""; } else { strbe = "<a class='num' href='" + url + "&p=0'>首页</a>" + "<a class='num' href='" + url + "&p=" + (pp - 2) + "'>上一页</a>" + strbe; }
                String strmid = "<span class='current'>" + pp + "</span>";
                String strlater = "";
                int pagelater = pp + beforlaterln;
                for (int i = pp; i < pagelater; i++)
                {
                    if (i < countpage)
                    {
                        strlater += "<a class='num' href='" + url + "&p=" + (i) + "'>" + (i + 1) + "</a>";
                    }
                }
                if (pp >= countpage) { } else { strlater += "<a class='num' href='" + url + "&p=" + pp + "'>下一页</a>" + "<a class='num' href='" + url + "&p=" + (countpage - 1) + "'>尾页</a>"; }
                page = strbe + strmid + strlater;
            }
            return "共" + countpage + "页" + page;
        }
        static void Main(string[] args)
        {
            Program pg = new Program();
            String str = pg.pagestr("http://www.abc.com?", 195, 11, 10, 8);
            Console.WriteLine(str);
        }
    }
}

运行后得到一下字符串

共20页<a class='num' href='http://www.abc.com?&p=0'>首页</a>
<a class='num' href='http://www.abc.com?&p=10'>上一页</a>
<a class='num' href='http://www.abc.com?&p=3'>4</a>
<a class='num' href='http://www.abc.com?&p=4'>5</a>
<a class='num' href='http://www.abc.com?&p=5'>6</a>
<a class='num' href='http://www.abc.com?&p=6'>7</a>
<a class='num' href='http://www.abc.com?&p=7'>8</a>
<a class='num' href='http://www.abc.com?&p=8'>9</a>
<a class='num' href='http://www.abc.com?&p=9'>10</a>
<a class='num' href='http://www.abc.com?&p=10'>11</a>
<span class='current'>12</span>
<a class='num' href='http://www.abc.com?&p=12'>13</a>
<a class='num' href='http://www.abc.com?&p=13'>14</a>
<a class='num' href='http://www.abc.com?&p=14'>15</a>
<a class='num' href='http://www.abc.com?&p=15'>16</a>
<a class='num' href='http://www.abc.com?&p=16'>17</a>
<a class='num' href='http://www.abc.com?&p=17'>18</a>
<a class='num' href='http://www.abc.com?&p=18'>19</a>
<a class='num' href='http://www.abc.com?&p=19'>20</a>
<a class='num' href='http://www.abc.com?&p=12'>下一页</a>
<a class='num' href='http://www.abc.com?&p=19'>尾页</a>

希望对大家有帮助,谢谢!

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • python分页

    用户1623964
  • JAVA分页

    用户1623964
  • php分页

    用户1623964
  • php分页

    用户1623964
  • JAVA分页

    用户1623964
  • python分页

    用户1623964
  • LeetCode 371. 两整数之和(位运算加法)

    不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。

    Michael阿明
  • SpringBoot系列之快速创建项目教程

    本博客简介一下SpringBoot快速创建工程的方法,主要介绍一下Spring Initializer,Spring Initializer是IntelliJ ...

    SmileNicky
  • Python代理设置

    py3study
  • [机器学习必知必会]凸优化

    凸优化问题(OPT,convex optimization problem)指定义在凸集中的凸函数最优化的问题。尽管凸优化的条件比较苛刻,但仍然在机器学习领域有...

    TOMOCAT

扫码关注云+社区

领取腾讯云代金券