zoukankan      html  css  js  c++  java
  • Asp.Net分页生成页码超链接方法

    namespace Common
    {
        public class PageLinkHelp
        {
            /// <summary>
            /// 生成分页超链接标签
            /// 使用了Bootstrap3的分页样式:首页«第380页第381页第382页第383页第384页»尾页
            /// </summary>
            /// <param name="pageIndex">当前页码(当前页索引)</param>
            /// <param name="pageCount">总页码数
            /// 总页数计算公式:
            /// 第1种.Math.Max((数据总条数 + 每页显示的数量 - 1) / 每页显示的数量, 1):
            /// var pageCount = Math.Max((count + pageSize - 1) / pageSize, 1);
            /// 第2种.(int)Math.Ceiling(数据总条数 * 1.00 / 每页显示的数量) :
            /// var pageCount = (int)Math.Ceiling(count * 1.00 / pageSize);
            /// </param>
            /// <returns>页码超链接</returns>
            public static string GetPageBar(int pageIndex, int pageCount)
            {
                if (pageCount <= 1)
                {
                    return string.Empty;
                }
                int start = (pageIndex - 2);
                if (start < 1)
                {
                    start = 1;
                }
                int end = (start + 4);
                if (end > pageCount)
                {
                    start += pageCount - end;
                    end = pageCount;
                }
                var linkStr = new System.Text.StringBuilder();
                linkStr.Append("<ul class="pagination">");
                if (pageIndex != 1)
                {
                    linkStr.Append($"<li class="page-item"><a class="page-link" href='?pageIndex={1}' >首页</a></li>");
                    linkStr.Append($"<li class="page-item"><a class="page-link" href='?pageIndex={pageIndex - 1}' >&laquo;</a></li>");
                }
                for (int i = start; i <= end; i++)
                {
                    if (i == pageIndex)
                    {
                        //active
                        //当前页
                        linkStr.Append($"<li class="page-item active"><a class="page-link" href='?pageIndex={i}' >第{i}页</a></li>");
                    }
                    else
                    {
                        linkStr.Append($"<li class="page-item"><a class="page-link" href='?pageIndex={i}' >第{i}页</a></li>");
                    }
                }
                if (pageIndex < pageCount)
                {
                    linkStr.Append($"<li class="page-item"><a class="page-link" href='?pageIndex={pageIndex + 1}' >&raquo;</a></li>");
                    linkStr.Append($"<li class="page-item"><a class="page-link" href='?pageIndex={pageCount}' >尾页</a></li>");
                }
                linkStr.Append("</ul>");
                return linkStr.ToString();
            }
        }
    }

    记录一下,方便以后用。。。感觉计算这些乱七八糟的。。。

  • 相关阅读:
    初识Redis
    一次kafka的offset回退事件及相关知识点
    接口透传
    看懂Oracle执行计划
    Oracle中merge into的使用
    动态规划算法:0/1背包问题 (0/1 Knapsack Problem)
    动态规划算法:硬币找零(Minimum Coin Change)
    链表:按照左右半区的方式重新组合单链表
    链表:删除链表中倒数第K个节点
    链表:合并两个有序链表
  • 原文地址:https://www.cnblogs.com/heheblog/p/net_study_20180730.html
Copyright © 2011-2022 走看看