asp.net数据分页方法

/// <summary>
        /// 数据分页方法
        /// </summary>
        /// <param name="PageIndex">当前页</param>
        /// <param name="PageSize">每页显示数量</param>
        /// <param name="PageCount">总数据</param>
        /// <param name="Url">链接,如:list.aspx?id=1234</param>
        /// <returns></returns>
        public static string GetPage(int PageIndex, int PageSize, int RecordCount, string Url)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                //计算总页数
                int PageCount = RecordCount % PageSize == 0 ? RecordCount / PageSize : RecordCount / PageSize + 1;
                if (PageIndex < 1)
                {
                    PageIndex = 1;
                }
                if (PageIndex > PageCount)
                {
                    PageIndex = PageCount;
                }
                string StarPage = "";//首页
                string EndPage = "";//尾页
                string PrePage = "";//上一页
                string NextPage = "";//下一页
                //首页和上一页的链接
                if (PageIndex <= 1 || PageCount <= 1)
                {
                    StarPage = "";
                    PrePage = "";
                }
                else
                {
                    StarPage = "";
                    PrePage = "<li class=\"previous\"><a href=\"" + Url + "&page=" + (PageIndex - 1) + "\">上一页</a></li>";
                }
                //末页和下一页的链接
                if (PageIndex == PageCount || PageCount <= 1)
                {
                    EndPage = "";
                    NextPage = "";
                }
                else
                {
                    EndPage = "";
                    NextPage = "<li class=\"next\"><a href=\"" + Url + "&page=" + (PageIndex + 1) + "\">下一页</a></li>";
                }
                //页码输出
                int PagerStart = 1;//第一个页码
                if (PageCount >= 5)
                {
                    PagerStart = PageIndex % 5 == 0 ? PageIndex - 2 : PageIndex - PageIndex % 5;
                }
                if (PagerStart < 1)
                {
                    PagerStart = 1;
                }
                string NumBtn = "";
                for (int i = PagerStart; i < PagerStart + 5 && i <= PageCount; i++)
                {
                    if (i == PageIndex)
                    {
                        NumBtn += "<li class=\"current\"><a>" + i + "</a>";
                    }
                    else
                    {
                        NumBtn += "<li><a href=\"" + Url + "&page=" + i + "\">" + i + "</a></li>";
                    }
                }
                sb.Append(StarPage + PrePage + NumBtn + NextPage + EndPage);
            }
            catch
            {
                sb.Append("");
            }
            return sb.ToString();
        }

具体样式可以去www.weixh.net参考一下

转载于:https://www.cnblogs.com/sanjuantianshu/p/3924591.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>