C#版生成HTML分页标签

public static string GetPaging(int recordCount, int pageIndex, int pageSize, string pageLink, string nextPageText, string prevPageText, string homePageText, string lastPageText, string displayMsg)
        {
            if (pageSize < 1) { pageSize = 1; }
            if (pageIndex < 1) { pageIndex = 1; }
            //开始
            string html = string.Empty;
            string htmlLeftPage = string.Empty;
            string htmlRightPage = string.Empty;
            if (pageLink == string.Empty) { pageLink = "?"; }
            //开始计算
            long pageCount = (recordCount + pageSize - 1)/pageSize;
            for (int i = 2; i >= 1; i--)
            {
                if (pageIndex - i >= 1)
                {
                    htmlLeftPage += string.Format("<a class=\"pageLink\" href=\"{0}\">{1}</a>", string.Format(pageLink, (pageIndex - i)), pageIndex - i);
                }
            }
            for (int j = 1; j <= 2; j++)
            {
                if (pageIndex + j <= pageCount)
                {
                    htmlRightPage += string.Format("<a class=\"pageLink\" href=\"{0}\">{1}</a>", string.Format(pageLink, (pageIndex + j)), pageIndex + j);
                }
            }
            long prevPage = pageIndex - 1;
            if (prevPage < 1) { prevPage = 1; }
            long nextPage = pageIndex + 1;
            if (nextPage > pageCount) { nextPage = pageCount; }
            //最后处理
            if (nextPage < 1) { nextPage = 1; }
            if (pageCount < 1) { pageCount = 1; }
            //计算结束
            string leftHtml = string.Format("<a class=\"pageLink\" {0}>{1}</a><a class=\"pageLink\" {2}>{3}</a>{4}", pageIndex != 1 ? string.Format("href=\"{0}\"", string.Format(pageLink, 1)) : "", homePageText, pageIndex != prevPage ? string.Format("href=\"{0}\"", string.Format(pageLink, prevPage)) : "", prevPageText, htmlLeftPage);
            string rightHtml = string.Format("{0}<a class=\"pageLink\" {1}>{2}</a><a class=\"pageLink\" {3}>{4}</a>", htmlRightPage, pageIndex != nextPage ? string.Format("href=\"{0}\"", string.Format(pageLink, nextPage)) : "", nextPageText, pageIndex != pageCount ? string.Format("href=\"{0}\"", string.Format(pageLink, pageCount)) : "", lastPageText);
            html = string.Format("<div class=\"pagerbox\">{0}<b>{1}</b>{2}</div><span>{3}</span>", leftHtml, pageIndex, rightHtml, string.Format(displayMsg, pageIndex, pageCount, recordCount));
            return html;
        }

                为了设置样式,我在每个标签里写了class="pageLink",大家可以根据需要修改。如果不设置样式,生成的标签有点紧凑,至少应该设置一下左右的margin属性。

            调用示例:

	int count = GetCount();//总记录数
	int pageCount = (count+pageSize-1)/pageSize;//注意这个求总页数的算法技巧
	Paging = Common.WebCommon.GetPaging(count, index, pageSize, "List.html?index={0}", "下一页", "上一页", "首页", "尾页", "当前第" + index + "页,共" + pageCount + "页,共" + count + "条帖子。");

              效果图:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值