封装成静态方法,方便使用,代码如下:
View Code
////// 分页控件 /// /// 分页大小 /// 当前页 /// 总记录数 ///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("首页", redirecTo, pageSize); } if (currentPage > 1) { //处理上一页 output.AppendFormat("上一页", 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("{3}", redirecTo, currentPage, pageSize, currentPage); } else { //处理其它页 output.AppendFormat("{3}", redirecTo, currentPage + i - currint, pageSize, currentPage + i - currint); } } output.Append(" "); } if (currentPage < totalPages) { //处理下一页 output.AppendFormat("下一页",redirecTo,currentPage+1,pageSize); } if (currentPage != totalPages) { //处理末页 output.AppendFormat("末页",redirecTo,totalPages,pageSize); } output.Append(" "); } output.AppendFormat("第{0}页/共{1}页", currentPage, totalPages);//统计 return output.ToString(); }