原文地址:http://www.cnblogs.com/hailibu/archive/2010/04/27/1721866.html
博客园里用的分页导航控件,我大胆地猜测应该是通过后台程序代码,然后在页面上输出html标签。我发现它与新浪的分页导航非常相像,不过后者是通过JavaScript创建分页导航。
这种分页导航有4种状态:
1、不带省略号的
2、省略号在右边
3、省略号在左边
4、省略号在两边
下面是我根据这4种状态,写出的分页导航代码,欢迎园友们多多指点^_^
CSS样式
#pager
{
font-size: 12px;
margin: 25px 0;
text-align: center;
color: #2E6AB1;
line-height: 200%;
}
#pager a
{
border: 1px solid #9AAFE5;
color: #2E6AB1;
margin-right: 4px;
padding: 2px 8px;
text-decoration: none;
}
#pager a:hover
{
border: 1px solid #2E6AB1;
color: #363636;
}
#pager span.current
{
background-color: #2E6AB1;
border: 1px solid navy;
color: #FFFFFF;
font-weight: bold;
margin-right: 4px;
padding: 2px 8px;
}
页面代码:
<div id="pager_block">
<div id='pager'>
分页导航Html标签存放位置
</div>
</div>
程序代码:
002 |
using System.Collections.Generic; |
012 |
/// <param name="pageIndex">索引页码,从1开始</param> |
013 |
/// <param name="pageCount">总页数</param> |
014 |
/// <param name="showPageCount">显示分页个数(奇数)</param> |
015 |
/// <returns></returns> |
016 |
public static string Build( int pageIndex, int pageCount, int showPageCount) |
021 |
StringBuilder sb = new StringBuilder(); |
023 |
int span = showPageCount / 2; |
027 |
if (pageCount > showPageCount + 1) |
029 |
if (pageIndex <= span + 1) |
031 |
from = 1; to = showPageCount; |
034 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>上一页</a>" , pageIndex - 1); |
036 |
sb.Append(ShowPageNavigation(pageIndex, from, to)); |
040 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>{0}</a>" , pageCount); |
042 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>下一页</a>" , pageIndex + 1); |
045 |
else if (pageIndex >= pageCount - span) |
047 |
from = pageCount + 1 - showPageCount; to = pageCount; |
049 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>上一页</a>" , pageIndex - 1); |
051 |
sb.Append( "<a href='Default.aspx'>1</a>" ); |
055 |
sb.Append(ShowPageNavigation(pageIndex, from, to)); |
057 |
if (pageIndex != pageCount) |
058 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>下一页</a>" , pageIndex + 1); |
062 |
from = pageIndex - span; to = pageIndex + span; |
064 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>上一页</a>" , pageIndex - 1); |
066 |
sb.Append( "<a href='Default.aspx'>1</a>" ); |
070 |
sb.Append(ShowPageNavigation(pageIndex, from, to)); |
074 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>{0}</a>" , pageCount); |
076 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>下一页</a>" , pageIndex + 1); |
082 |
from = 1; to = pageCount; |
085 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>上一页</a>" , pageIndex - 1); |
087 |
sb.Append(ShowPageNavigation(pageIndex, from, to)); |
089 |
if (pageIndex != pageCount) |
090 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>下一页</a>" , pageIndex + 1); |
093 |
return sb.ToString(); |
096 |
private static string ShowPageNavigation( int pageIndex, int from, int to) |
098 |
StringBuilder sb = new StringBuilder(); |
100 |
for ( int i = from; i <= to; i++) |
104 |
sb.AppendFormat( "<span class='current'>{0}</span>" , i); |
108 |
sb.AppendFormat( "<a href='Default.aspx?page={0}'>{0}</a>" , i); |
112 |
return sb.ToString(); |