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参考一下

asp.net数据分页方法,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。