C#版 分页导航条

封装成静态方法,方便使用,代码如下:

View Code
/// <summary>
        /// 分页控件
        /// </summary>
        /// <param name="pageSize">分页大小</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="totalCount">总记录数</param>
        /// <returns></returns>
        public static string ShowPageNavigate(int pageSize, int currentPage, int totalCount)
        {
            string redirecTo="";//跳转的页面
            //如果没有pageSize,默认设置为3
            pageSize = pageSize == 0 ? 3 : pageSize;
            int totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1);//总分页数
            var output = new StringBuilder();
            if (totalPages > 1)
            {
                if (currentPage != 1)
                {//处理首页链接                    
                    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a>", redirecTo, pageSize);
                }
                if (currentPage > 1)
                { //处理上一页
                    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a>", redirecTo, currentPage - 1, pageSize);
                }
                output.Append(" ");
                int currint = 5;
                for (int i = 0; i < 10; i++)
                {//一共最多显示10个页码
                    if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
                    {//处理当前页面的前面5个,后面5个
                        if (currentPage == (currentPage + i - currint))
                        { //处理当前页
                            output.AppendFormat("<a class='cpb' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a>", redirecTo, currentPage, pageSize, currentPage);
                        }
                        else
                        {//处理其它页
                            output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a>", redirecTo, currentPage + i - currint, pageSize, currentPage + i - currint);
                        }
                    }
                    output.Append(" ");
                }
                if (currentPage < totalPages)
                { //处理下一页
                    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a>",redirecTo,currentPage+1,pageSize);
                }
                if (currentPage != totalPages)
                { //处理末页
                    output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a>",redirecTo,totalPages,pageSize);
                }
                output.Append(" ");
            }
            output.AppendFormat("第{0}页/共{1}页", currentPage, totalPages);//统计
            return output.ToString();
        }

 

posted @ 2013-03-28 17:04  海藻  阅读(426)  评论(0编辑  收藏  举报