您的位置:首页 > Web前端 > CSS

自己写的一个生成html分页条的类和css

2011-06-19 17:56 274 查看
字节写了一个创建分页条的类:

/// <summary>
/// 生成分页标签
/// </summary>
public class Pager
{
private int pageIndex;
/// <summary>
/// 当前请求的页码
/// </summary>
public int PageIndex
{
get { return pageIndex; }
private set { pageIndex = value; }
}

private int pageSize;
/// <summary>
/// 请求的当前页的大小
/// </summary>
public int PageSize
{
get { return pageSize; }
private set { pageSize = value; }
}

private int rowCount;
/// <summary>
/// 数据总量,可以根据这个判断出能够显示多少页
/// </summary>
public int RowCount
{
get { return rowCount; }
private set { rowCount = value; }
}

private string hrefFormat;
/// <summary>
/// 页码连接所要指向的url格式化字符串例如:aticleList.aspx?{0}&delegate=wenxue
/// </summary>
public string HrefFormat
{
get { return hrefFormat; }
set { hrefFormat = value; }
}
private int pageCount;

private Pager() { }

public Pager(int pageIndex, int pageSize, int rowCount)
{
//计算出总共有多少页
this.pageCount = (int)Math.Ceiling(rowCount / (double)pageSize);
//如果pageIndex<1 就让当前页码等于1,如果pageIndex大于最大页码.
this.PageIndex = GetPageIndex(pageIndex, pageSize, rowCount);

this.PageSize = pageSize;
}

public string CreatePager()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("<div class='pager'>");
if (this.PageIndex > 1)
{
sb.AppendLine("\t<a id='pre' href='" + string.Format(this.HrefFormat, "page=" + GetPageIndex(this.pageIndex - 1, this.PageSize, this.RowCount)) + "'>" + "上一页" + "</a>");
}
else
{
sb.AppendLine("\t<a id='pre' class='false'>" + "上一页" + "</a>");
}
CreatePageIndex(sb);
if (this.pageIndex < this.pageCount)
{
sb.AppendLine("\t<a id='next' href='" + string.Format(this.HrefFormat, "page=" + GetPageIndex(this.pageIndex + 1, this.PageSize, this.RowCount)) + "'>" + "下一页" + "</a>");
}

sb.AppendLine("</div>");
return sb.ToString();
}

private void CreatePageIndex(StringBuilder sb)
{
if (this.pageIndex <= 7)
{
int i = 1;
for (; i <= (this.pageIndex > this.pageCount ? 10 : this.pageCount); i++)
{
if (i == this.pageIndex)
{
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "' class='Active'>" + i + "</a>");
continue;
}
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "'>" + i + "</a>");
}
if (i < this.pageCount)
{
sb.AppendLine("...");
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + this.pageCount + "") + "'>" + this.pageCount + "</a>");

}
}
else if (this.PageIndex > 12 && this.pageCount > this.pageIndex + 5)
{
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=1") + "'>" + 1 + "</a>");
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=2") + "'>" + 2 + "</a>");
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=3") + "'>" + 3 + "</a>");
sb.AppendLine("...");
for (int i = this.pageIndex - 4; i <= (this.pageIndex + 4 > this.pageCount ? this.pageCount : this.pageIndex + 4); i++)
{
if (i == this.pageIndex)
{
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "' class='Active'>" + i + "</a>");
continue;
}
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "'>" + i + "</a>");
}
sb.AppendLine("...");
for (int j = this.pageCount - 2; j <= this.pageCount; j++)
{
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + j) + "'>" + j + "</a>");
}
}
else if (this.pageIndex > 7)
{

int i = this.pageIndex - 4;
if (i > 2)
{
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=1") + "'>" + 1 + "</a>");
sb.AppendLine("...");
}
for (; i <= (this.pageIndex + 4 > this.pageCount ? this.pageCount : this.pageIndex + 4); i++)
{
if (i == this.pageIndex)
{
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "' class='Active'>" + i + "</a>");
continue;
}
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + i) + "'>" + i + "</a>");

}
if (i < this.pageCount)
{
sb.AppendLine("...");
sb.AppendLine("\t<a href='" + string.Format(HrefFormat, "page=" + this.pageCount + "") + "'>" + this.pageCount + "</a>");
}
}
}

private int GetPageIndex(int pageIndex, int pageSize, int rowCount)
{
int pageCount = (int)Math.Ceiling(rowCount / (double)pageSize);
if (pageIndex < 1)
{
return 1;
}
else if (pageIndex > this.pageCount)//就让他等于最大页码
{
return pageCount;
}
else
{
return pageIndex;
}
}
}


css:

div.pager a
{
display:block;
float:left;
margin-left:3px;
text-align:center;
text-decoration:none;
border:1px solid Gray;
color:Gray;
width:26px;
}
#pre
{
width:80px;
}
#pre.false:hover
{
border:1px solid Gray;
color:Gray;
background-Color:White;
}
#next
{
width:80px;
}
div.pager a:hover,div.pager a.Active
{
border:1px solid black;
background-Color:#4b6c9e;
color:#f9f9f9;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: