asp.net数据分页方法

8/10/2015来源:ASP.NET技巧人气:1241

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