c#分页代码

        /// <summary> 
        /// 分页函数 
        /// </summary> 
        /// <param name="rowsCount">总记录数</param> 
        /// <param name="pageSize">每页记录数</param> 
        /// <param name="currentPage">当前页数</param> 
        /// <param name="url">Url参数</param> 
        /// <returns></returns> 
        public string Pagination(int rowsCount, int pageSize, int currentPage, string url)
        {
            int allCurrentPage = 0;

            if (currentPage < 1)
            {
                currentPage = 1;
            }

            //计算总页数 
            if (pageSize != 0)
            {
                allCurrentPage = (rowsCount / pageSize);
                allCurrentPage = ((rowsCount % pageSize) != 0 ? allCurrentPage + 1 : allCurrentPage);
                allCurrentPage = (allCurrentPage == 0 ? 1 : allCurrentPage);
            }

            int next = currentPage + 1;
            int pre = currentPage - 1;
            int startCount = (currentPage + 5) > allCurrentPage ? allCurrentPage - 9 : currentPage - 4;

            //中间页终止序号 
            int endcount = currentPage < 5 ? 10 : currentPage + 5;

            //为了避免输出的时候产生负数,设置如果小于1就从序号1开始 
            if (startCount < 1)
            {
                startCount = 1;
            }

            //页码+5的可能性就会产生最终输出序号大于总页码,那么就要将其控制在页码数之内 
            if (allCurrentPage < endcount)
            {
                endcount = allCurrentPage;
            }

            string currentPageStr = "" + allCurrentPage + "";

            currentPageStr += currentPage > 1 ? "<a href=\"" + url + "?page=1\">首页</a>  <a href=\"" + url + "?page=" + pre + "\">上一页</a>" : "首页 上一页";

            //中间页处理,这个增加时间复杂度,减小空间复杂度 
            for (int i = startCount; i <= endcount; i++)
            {
                currentPageStr += currentPage == i ? "  <font color=\"#ff0000\">" + i + "</font>" : "  <a href=\"" + url + "?page=" + i + "\">" + i + "</a>";
            }

            currentPageStr += currentPage != allCurrentPage ? "  <a href=\"" + url + "?page=" + next + "\">下一页</a>  <a href=\"" + url + "?page=" + allCurrentPage + "\">末页</a>" : " 下一页 末页";
            return currentPageStr;
        }

 

转载于:https://www.cnblogs.com/wzh13681626019/p/3151880.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值