ASP.NET 生成分页HTML标签

效果如下:





程序代码:

#region 分页代码
        public int pPageSize = 5;
        public int pPageIndex = 1;
        public int pPageCount = 0;
        /// <summary>
        /// 获取分页HTML代码
        /// </summary>
        /// <param name="pPageCount"></param>
        /// <param name="pPageIndex"></param>
        /// <param name="pParames"></param>
        /// <returns></returns>
        public string GetPageHtml(int pPageCount, int pPageIndex, int pPageSize, string pPageName, string pParames)
        {
            if (pPageIndex <= 0)
            {
                pPageIndex = 1;
            }
            if (pPageSize <= 0)
            {
                pPageSize = 10;
            }

            StringBuilder strHtml = new StringBuilder();
            strHtml.Append("<div class=\"nub_guide\">");

            if (pPageIndex > 1)
            {
                strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">首页</a>", pPageName
                                , pPageSize
                                , 1
                                , string.IsNullOrEmpty(pParames) ? "" : "&" + pParames));
            }

            if (pPageIndex > 1)
            {
                strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">上一页</a>", pPageName
                                  , pPageSize
                                  , pPageIndex - 1
                                  , string.IsNullOrEmpty(pParames) ? "" : "&" + pParames));
            }
            int i = pPageIndex; //起始页
            int Count = pPageCount;  //循环的页码

            //设置分页起始页
            if ((pPageIndex - 1) % 5 == 0)
            {
                i = pPageIndex;
            }
            else
            {
                i = pPageIndex - ((pPageIndex - 1) % 5);
            }

            if ((pPageIndex - 1) % 5 == 0 && (pPageCount - pPageIndex + 1) % 5 == 0)
            {
                Count = pPageIndex + 4;
            }
            else
            {
                Count = pPageIndex + ((pPageCount - pPageIndex + 1) % 5);
            }

            if (Count > pPageCount)
            {
                Count = pPageCount;
            }

            for (; i <= Count; i++)
            {
                //当前页
                if (i == pPageIndex)
                {
                    strHtml.Append(string.Format("<a href=\"#\" class=\"now\" >{0}</a>", i.ToString()));
                }
                else
                {
                    //其他页
                    strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">{4}</a>", pPageName
                                      , pPageSize
                                      , i
                                      , string.IsNullOrEmpty(pParames) ? "" : "&" + pParames
                                      , i.ToString()));
                }
            }

            if (pPageIndex < pPageCount)
            {
                strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">下一页</a>", pPageName
                             , pPageSize
                             , pPageIndex + 1
                             , string.IsNullOrEmpty(pParames) ? "" : "&" + pParames));
            }

            if (pPageIndex < pPageCount)
            {
                strHtml.Append(string.Format("<a href=\"{0}?pageSize={1}&pageIndex={2}{3}\">尾页</a>", pPageName
                                , pPageSize
                                , pPageCount
                                , string.IsNullOrEmpty(pParames) ? "" : "&" + pParames));
            }
            strHtml.Append("</div>");
            return strHtml.ToString();
        }
        #endregion


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值