自己写的一个生成html分页条的类和css
2011-06-19 17:56
274 查看
字节写了一个创建分页条的类:
css:
/// <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; }
相关文章推荐
- 利用ps生成html代码,如下图自己写css会不精确,可以利用ps生成
- Asp.Net控件DataGrid生成的HTML为一个Table,这点和Div+CSS矛盾
- 附上曾经自己埋头苦写的HTML+CSS+JS 的成品集 <打地鼠>
- 妙味课堂:css学习--- 制作一个网页html+css过程
- 创建一个自己的动态HTML-备
- 一个用ASP生成html的新方法
- 动态生成一个HTML对象
- 技巧和诀窍:使用ASP.NET 2.0 CSS 控件适配器生成CSS友好的HTML输出
- 自己开发的一个非常粗糙的HTML格式化工具
- 工作总结 @Html 辅助方法 为 生成的 标签设置元素属性 htmlAttributes 一个对象,其中包含要为该元素设置的 HTML 特性。
- 用html css javascript打造自己的RIA图文教程第1/2页
- C源码:生成一个自己的口令字典
- 写给自己看的HTML、CSS、XHTML指南 七
- java类生成日历自己写的,可以输出到html页面
- 弄到现在才知道网页没有combobox,弄网上的服务器控件不方便,自己用textbox+dropdownlist用CSS组合起一个简单的combobox效果。
- 织梦dedecms生成栏目HTML缓慢的一个可能原因及解决方法
- [置顶] 【实战】如何通过html+css+mysql+php来快速的制作动态网页(以制作一个博客网站为列)
- [CSS]自己的一个标准写法?
- 附上曾经自己埋头苦写的HTML+CSS+JS 的作品集 <坦克大战>
- 使用html+css实现一个个人照片墙