asp.net实现分页(非控件,输出html代码)

本分页来自DTcms的分页代码,其中链接地址可根据项目需要自行拼接。

代码如下:

        /// <summary>
        /// 返回分页页码
        /// </summary>
        /// <param name="pageSize">页面大小</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="totalCount">总记录数</param>
        /// <param name="linkUrl">链接地址</param>
        /// <param name="centSize">中间页码数量</param>
        /// <returns></returns>
        public static string OutPageList(int pageSize, int pageIndex, int totalCount, string linkUrl, int centSize)
        {
            //计算页数 若总记录数、页数小于1 则不显示分页
            if (totalCount < 1 || pageSize < 1)
            {
                return "";
            }

            //计算总页数
            int pageCount = totalCount / pageSize;

            //如果页数小于1 则不显示分页
            if (pageCount < 1)
            {
                return "";
            }

            //总记录数不被页面大小整除  则总页数+1
            if (totalCount % pageSize > 0)
            {
                pageCount += 1;
            }
            if (pageCount <= 1)
            {
                return "";
            }
            StringBuilder pageStr = new StringBuilder();
            string firstBtn = "<a href=\"" + linkUrl, (pageIndex - 1).ToString() + "\">«上一页</a>";
            string lastBtn = "<a href=\"" + linkUrl,  (pageIndex + 1).ToString() + "\">下一页»</a>";
            string firstStr = "<a href=\"" + linkUrl,  "1" + "\">1</a>";
            string lastStr = "<a href=\"" + linkUrl,  pageCount.ToString() + "\">" + pageCount.ToString() + "</a>";

            //当前页<=1 上一页按钮不可用
            if (pageIndex <= 1)
            {
                firstBtn = "<span class=\"disabled\">«上一页</span>";
            }

            //当前页>=总页数 下一页按钮不可用
            if (pageIndex >= pageCount)
            {
                lastBtn = "<span class=\"disabled\">下一页»</span>";
            }

            //当前页为1 时 选中页码1
            if (pageIndex == 1)
            {
                firstStr = "<span class=\"current\">1</span>";
            }

            //当前页为页面总数 时 选中最后页码
            if (pageIndex == pageCount)
            {
                lastStr = "<span class=\"current\">" + pageCount.ToString() + "</span>";
            }

            //中间开始的页码
            int firstNum = pageIndex - (centSize / 2); 
            if (pageIndex < centSize)
                firstNum = 2;

            //中间结束的页码
            int lastNum = pageIndex + centSize - ((centSize / 2) + 1); 
            if (lastNum >= pageCount)
                lastNum = pageCount - 1;
            pageStr.Append("<span>共" + totalCount + "记录</span>");
            pageStr.Append(firstBtn + firstStr);
            if (pageIndex >= centSize)
            {
                pageStr.Append("<span>...</span>\n");
            }
            for (int i = firstNum; i <= lastNum; i++)
            {
                if (i == pageIndex)
                {
                    pageStr.Append("<span class=\"current\">" + i + "</span>");
                }
                else
                {
                    pageStr.Append("<a href=\"" + linkUrl, pageId, i.ToString() + "\">" + i + "</a>");
                }
            }
            if (pageCount - pageIndex > centSize - ((centSize / 2)))
            {
                pageStr.Append("<span>...</span>");
            }
            pageStr.Append(lastStr + lastBtn);
            return pageStr.ToString();
        }
 

asp.net实现分页(非控件,输出html代码)


欢迎分享,(木庄网络博客交流QQ群:562366239

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论