应用截图如下:
只需提供当前页数和总页数就可以创建出如上的分页链接!
先看下PagedLinkBuilder的使用:
先引用OPS.Core.dll到项目中:下载地址:http://lib.ops.cc/dowload/ops.core
01 |
using OPS.Web.UI;
|
02 |
public class Page
|
03 |
{ |
04 |
public string GetPagerHtml()
|
05 |
{
|
06 |
int currentPageIndex = int.Parse(Request["page"]); int pageCount = 50;
|
07 |
//假设有50页
|
08 |
PagedLinkBuilder p = new PagedLinkBuilder(currentPageIndex, pageCount);
|
09 |
//以下属性均为可选
|
10 |
p.LinkCount = 5; //链接个数
|
11 |
p.EnableInput = true; //是否使用输入页码框
|
12 |
p.NextPageText = "下一页>>";
|
13 |
p.PreviousPageText = "<<上一页";
|
14 |
//返回生成的分页链接Html
|
15 |
return p.ToString("?domain=ops.cc&page={0}");
|
16 |
}
|
17 |
} |
你可以打开浏览器查看输出的HTML代码
PagedLinkBuilder源代码如下:
001 |
/* |
002 |
* PagedLinksBuilder 分页链接构造器
|
003 |
* Copyright 2010 OPS,All right reseved!
|
004 |
* Newmin(ops.cc) @ 2010/11/18
|
005 |
*/
|
006 |
namespace OPS.Web.UI
|
007 |
{ |
008 |
using System;
|
009 |
using System.Text;
|
010 |
using System.Text.RegularExpressions;
|
011 |
|
012 |
/// <summary>
|
013 |
/// 分页链接创建器(url:www.ops.cc)
|
014 |
/// </summary>
|
015 |
public class PagedLinkBuilder:IFormattable
|
016 |
{
|
017 |
public PagedLinkBuilder() { }
|
018 |
public PagedLinkBuilder(int currentPageIndex, int pageCount)
|
019 |
{
|
020 |
CurrentPageIndex = currentPageIndex;
|
021 |
PageCount = pageCount;
|
022 |
}
|
023 |
/// <summary>
|
024 |
/// 当前页面索引
|
025 |
/// </summary>
|
026 |
public int CurrentPageIndex { get; set; }
|
027 |
/// <summary>
|
028 |
/// 页面总数
|
029 |
/// </summary>
|
030 |
public int PageCount { get; set; }
|
031 |
/// <summary>
|
032 |
/// 上一页文字
|
033 |
/// </summary>
|
034 |
public string PreviousPageText { get; set; }
|
035 |
/// <summary>
|
036 |
/// 下一页文字
|
037 |
/// </summary>
|
038 |
public string NextPageText { get; set; }
|
039 |
/// <summary>
|
040 |
/// 链接长度,创建多少个跳页链接
|
041 |
/// </summary>
|
042 |
public int? LinkCount { get; set; }
|
043 |
/// <summary>
|
044 |
/// 是否允许输入页码调页
|
045 |
/// </summary>
|
046 |
public bool EnableInput { get; set; }
|
047 |
|
048 |
/// <summary>
|
049 |
/// 输入分页链接HTML代码
|
050 |
/// </summary>
|
051 |
/// <param name="format">例如:?domain=ops.cc&page={0},{0}将会被解析成页码</param>
|
052 |
/// <param name="formatProvider"></param>
|
053 |
/// <returns></returns>
|
054 |
public string ToString(string format, IFormatProvider formatProvider)
|
055 |
{
|
056 |
//创建匹配{page}的Regex对象
|
057 |
Regex reg=new Regex("{(\\d)+}");
|
058 |
//如果不包括匹配则抛出异常
|
059 |
if(!reg.IsMatch(format)) throw new ArgumentException("表达区中页数需要使用{[1,2,3....]}表示");
|
060 |
//计算上一页和下一页
|
061 |
int pindex=CurrentPageIndex<=1?1:CurrentPageIndex-1;
|
062 |
int nindex=CurrentPageIndex >= PageCount ? PageCount:CurrentPageIndex+1;
|
063 |
|
064 |
StringBuilder sb = new StringBuilder();
|
065 |
sb.Append("<a class=\"previous\" href=\"").Append(pindex==1?"##":reg.Replace(format,pindex.ToString()))
|
066 |
.Append("\">").Append(PreviousPageText??"<<上一页").Append("</a>");
|
067 |
|
068 |
//起始页:CurrentPageIndex / 10 * 10+1
|
069 |
//结束页:(CurrentPageIndex%10==0?CurrentPageIndex-1: CurrentPageIndex) / 10 * 10
|
070 |
//当前页数能整除10的时候需要减去10页,否则不能选中
|
071 |
|
072 |
int c = LinkCount ?? 10;
|
073 |
for (int i = 1, j = CurrentPageIndex /c * c+1; i<=c;
|
074 |
i++, j =(CurrentPageIndex%c==0?CurrentPageIndex-1: CurrentPageIndex) /c * c+ i)
|
075 |
{
|
076 |
sb.Append("<a href=\"").Append(j==CurrentPageIndex?"#\" class=\"current":reg.Replace(format,j.ToString())).Append("\">")
|
077 |
.Append(j.ToString()).Append("</a>");
|
078 |
}
|
079 |
//使用输入页码框
|
080 |
if (EnableInput)
|
081 |
sb.Append("<input type=\"text\" size=\"3\"/><input type=\"button\" onclick=\"var _page=parseInt(this.previousSibling.value);if(_page)location.href='")
|
082 |
.Append(reg.Replace(format,"__page__")).Append("'.replace('__page__',_page);\" value=\"Go\"/>");
|
083 |
|
084 |
sb.Append("<a class=\"next\" href=\"").Append(nindex == PageCount ? "##" : reg.Replace(format, nindex.ToString()))
|
085 |
.Append("\">").Append(NextPageText ?? "下一页>></a>");
|
086 |
return sb.ToString();
|
087 |
}
|
088 |
/// <summary>
|
089 |
/// 输入分页链接HTML代码
|
090 |
/// </summary>
|
091 |
/// <param name="format">例如:?domain=ops.cc&page={0},{0}将会被解析成页码</param>
|
092 |
public string ToString(string format)
|
093 |
{
|
094 |
return ToString(format, null);
|
095 |
}
|
096 |
public override string ToString()
|
097 |
{
|
098 |
return ToString("?pageIndex={0}");
|
099 |
}
|
100 |
}
|
101 |
} |
转载请注明出处:http://lib.ops.cc
http://www.cnblogs.com/anet/archive/2010/11/18/ops-lib-Paged-Link-Builder.html