自动组建分页页码(多参数)
先找个代码充充门面,不写日志的淫你们伤不起啊 以下代码将组建出整个分页控件的带参结构,全部为a标签
//****************
//coding by abel
//2011.03.07
//***************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
public class PagerTools
{
/// <summary>
/// (SEO)动态组件分页控件
/// </summary>
/// <param name="pageTotalCount">总页数</param>
/// <param name="showPageCount">显示多少个页码</param>
/// <param name="redirectPage">需跳转页的名字(应用当前页)</param>
/// <param name="URLParams">自定义参数(可添加任意多个) 必须包含PageIndex键值(页码参数以及当前页的值)</param>
/// <returns>字符串 Litera接收</returns>
public static string BuildPager(int pageTotalCount, int showPageCount, string redirectPage, params URLParam[] URLParams)
{
URLParam urlParmPageIndex = URLParams.SingleOrDefault(p => p.Key == "PageIndex");
if (urlParmPageIndex == null)
{
throw new Exception("URLParmas中未包含PageIndex键值");
}
//声明当前展示页的页码
int pageIndex = 0;
bool pageIndexCheck = int.TryParse(urlParmPageIndex.Value, out pageIndex);
if (!pageIndexCheck)
{
throw new Exception("PageIndex值不合法");
}
if (pageIndex > pageTotalCount || pageIndex <= 0)
{
throw new Exception("当前页不能大于总页数且不能小于0");
}
//声明页面获取当前页页码的参数
string pageParam = urlParmPageIndex.Key;
StringBuilder sbUrlParams = new StringBuilder();
if (URLParams.Length != 0)
{
foreach (URLParam parm in URLParams)
{
if(parm.Key!="PageIndex")
sbUrlParams.AppendFormat("&{0}={1}",parm.Key,parm.Value);
}
}
if (pageTotalCount == 1)
return "";
StringBuilder sb = new StringBuilder();
int begin, go;
int average = showPageCount / 2;
if (pageTotalCount > showPageCount + 1)
{
if (pageIndex <= average + 1)
{
begin = 1; go = showPageCount;
if (pageIndex != 1)
sb.AppendFormat("<a href='{0}?{1}={2}{3}'><<上一页</a>",
redirectPage,
pageParam,
pageIndex - 1,
sbUrlParams);
sb.Append(ShowNavigation(pageIndex,begin, go, redirectPage, pageParam, sbUrlParams.ToString()));
sb.Append("...");
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>{2}</a>",
redirectPage,
pageParam,
pageTotalCount,
sbUrlParams);
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>下一页>></a>",
redirectPage,
pageParam,
pageIndex + 1,
sbUrlParams);
}
else if (pageIndex >= pageTotalCount - average)
{
begin = pageTotalCount + 1 - showPageCount; go = pageTotalCount;
sb.AppendFormat("<a href='{0}?{1}={2}{3}'><<上一页</a>",
redirectPage,
pageParam,
pageIndex - 1,
sbUrlParams);
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>1</a>",
redirectPage,
pageParam,1,
sbUrlParams);
sb.Append("...");
sb.Append(ShowNavigation(pageIndex,
begin,
go,
redirectPage,
pageParam,
sbUrlParams.ToString()));
if (pageIndex != pageTotalCount)
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>下一页>></a>",
redirectPage,
pageParam,
pageIndex + 1,
sbUrlParams);
}
else
{
begin = pageIndex - average;
go = pageIndex + average;
sb.AppendFormat("<a href='{0}?{1}={2}{3}'><<上一页</a>",
redirectPage,
pageParam,
pageIndex - 1,
sbUrlParams);
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>1</a>",
redirectPage,
pageParam, 1,
sbUrlParams);
sb.Append("...");
sb.Append(ShowNavigation(pageIndex,
begin,
go,
redirectPage,
pageParam,
sbUrlParams.
ToString()));
sb.Append("...");
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>{2}</a>",
redirectPage,
pageParam,
pageTotalCount,
sbUrlParams);
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>下一页>></a>",
redirectPage,
pageParam,
pageIndex + 1,
sbUrlParams);
}
}
else
{
begin = 1; go = pageTotalCount;
if (pageIndex != 1)
sb.AppendFormat("<a href='{0}?{1}={2}{3}'><<上一页</a>",
redirectPage,
pageParam,
pageIndex - 1,
sbUrlParams);
sb.Append(ShowNavigation(pageIndex,
begin,
go,
redirectPage,
pageParam,
sbUrlParams.
ToString()));
if (pageIndex != pageTotalCount)
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>下一页>></a>",
redirectPage,
pageParam,
pageIndex + 1,
sbUrlParams);
}
return sb.ToString();
}
private static string ShowNavigation(int pageIndex, int from, int to, string redirectPage, string pageParam, string sbUrlParams)
{
StringBuilder sb = new StringBuilder();
for (int i = from; i <= to; i++)
{
if (i == pageIndex)
{
sb.AppendFormat("<span class='select'>{0}</span>", i);
}
else
{
sb.AppendFormat("<a href='{0}?{1}={2}{3}'>{2}</a>",
redirectPage,
pageParam,
i,
sbUrlParams);
}
}
return sb.ToString();
}
}
public class URLParam
{
public string Key { get; set; }
public string Value { get; set; }
/// <summary>
///
/// </summary>
/// <param name="key">键</param>
/// <param name="value">值</param>
public URLParam(string key,string value)
{
this.Key = key;
this.Value = value;
}
}
页面引用如下CSS
#pager{
font-size: 12px; margin: 25px 0;
text-align: center; color: #9D9D9D;
line-height: 200%; } #pager a {
border: 1px solid #9D9D9D; color: black;
margin-right: 4px; padding: 2px 8px;
text-decoration: none; } #pager a:hover {
border: 1px solid red; color: red; }
#pager span.select{
background-color: #9D9D9D;
border: 1px solid navy; color: #FFFFFF;
font-weight: bold; margin-right: 4px;
padding: 2px 8px; } #pager span.redirect {
font-size: 13px; border: 1px solid #9D9D9D;
margin-right: 3px; padding: 1px 7px;
color: black; } #pager input {
border: 1px solid #9D9D9D; width: 30px; }