post

C#实现Google样式分页

废话不说了,直接上代码:下面附加样式文件

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
namespace MrCantus.Builder
{
     /// <summary>
     /// 分页字符串构建类
     /// Author:Zhang Heng
     /// Date:2009-9-21 21:35
     /// </summary>
     public class PagerBuilder
     {
          /// <summary>
          /// 构建分页字符串,返回一个带HTML代码的分页样式,可以用label在页面显示,
          也可以用this.Controls.Add(new LiteralControl(string));方法加入到页面控件中
          /// </summary>
          /// <param name="total">总记录数</param>
          /// <param name="pageSize">每页显示数目</param>
          /// <param name="currentPage">当前页码</param>
          /// <param name="thisPage">页面名称及以?分割的地址栏参数</param>
          /// <returns>字符串</returns>
          public static string Pager(int total, int pageSize, int currentPage, Page thisPage)
          {
               if (total <= pageSize)
               {
                    return "";
               }
               //分页字符串
               StringBuilder pagerString = new StringBuilder("<ul class=\"ulPager\">"); 
               int allPages = 0; //总页数
               int previousPage = 0; //上一页页码
               int nextPage = 0; //下一页页码
               int beginCount = 0; //当前页前面的页数
               int endCount = 0; //当前页后面的页数
               string page = ""; //地址栏分页参数
               //获取页面地址栏及?分割的参数
               string pageName = null == thisPage ?"":thisPage.Request.Url.PathAndQuery;
               //判断页面地址栏是否已含有参数
               page = pageName.Contains("?") ? pageName.Contains("page=") ? 
                    pageName.Substring(0, pageName.IndexOf("page=") + 5) : pageName 
                    + "&page=" : pageName + "?page=";
               //判断当前页
               currentPage = currentPage < 1 ? 1 : currentPage;
               //计算总页数
               if (pageSize > 0)
               {
                    allPages = total / pageSize;
                    allPages = total % pageSize != 0 ? allPages + 1 : allPages;
                    allPages = allPages == 0 ? 1 : allPages;
               }
               previousPage = currentPage - 1;
               nextPage = currentPage + 1;
               //中间页起始序号
               beginCount = currentPage + 5 > allPages ? allPages - 9 : currentPage - 4;
               //中间页终止序号
               endCount = currentPage < 5 ? 10 : currentPage + 5;
               //为了避免产生负数,如果小于1则从序号1开始
               beginCount = beginCount < 1 ? 1 : beginCount;
               //页码+5可能超出总页数,要将其控制在总页数之内
               endCount = endCount >= allPages ? allPages : endCount;
               //判断是否为首页
               pagerString.Append(currentPage > 1 ? "<li><a href=\"" 
                    + page + "1\">FIRST</a></li><li><a href=\"" 
                    + page + previousPage + "\">PREVIOUS</a></li>" : null);
               //中间页处理,这个增加时间复杂度,减小空间复杂度
               for (int i = beginCount; i <= endCount; i++)
               {
                    pagerString.Append(currentPage == i ? 
                    "<li><a class=\"currentPage\" href=\"" 
                         + page + currentPage + "\">" + i 
                         + "</a></li>" : "<li><a href=\"" 
                         + page + i + "\">" + i + "</a></li>");
               }
               //判断是否为末页
               pagerString.Append(currentPage != allPages ? "<li><a href=\"" 
                    + page + nextPage + "\">NEXT</a><li><a href=\"" 
                    + page + allPages + "\">LAST</a></li>" : null);
               //结束标记
               pagerString.Append("</ul><div></div>");
               return pagerString.ToString();
          }
     }
}
/*----------pager begin----------*/
.ulPager
{
margin: 20px 5%;
}
.ulPager li
{
list-style: none;
display: inline;
}
.ulPager li a
{
display: block;
float: left;
padding: 3px 6px;
border: solid 1px #C6E5F2;
background-color: #DEF1F9;
margin: 2px;
}
.ulPager li a:hover
{
border: solid 1px #007FA9;
text-decoration: none;
}
.ulPager li .currentPage
{
color: #ffffff;
background-color: #4A6969;
}
/*----------pager end----------*/
· 729 次浏览