重写的一个带分页的repeater
重写repeater 增加分页的功能
效果图:
代码如下,如果觉得repeater不好的话,把分页的部分提出来:
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace Ncuhome.Blog.Controls
{
public class RepeaterPaged : System.Web.UI.WebControls.Repeater
{
public RepeaterPaged()
{
}
private string _PageLocation;
/// <summary>
/// 分页码的显示地方
/// Top 顶端
/// End 底端
/// TopandEnd 上面下面都有
/// </summary>
[DefaultValue("End")]
public string PageLocation
{
get { return _PageLocation; }
set { this._PageLocation = value; }
}
private string _UrlFormat;
/// <summary>
/// 地址 example"more.aspx?id=1&"
/// </summary>
public string UrlFormat
{
get { return _UrlFormat; }
set { _UrlFormat = value; }
}
private long _Count;
/// <summary>
/// 总数
/// </summary>
public long Count
{
get { return _Count; }
set { _Count = value; }
}
private long _Length;
/// <summary>
/// 每页显示条数
/// </summary>
public long Length
{
get { return _Length; }
set { _Length = value; }
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
int PageIndex = 1;
if (string.IsNullOrEmpty(Context.Request.QueryString["PageIndex"]))
{
PageIndex = 1;
}
else
{
try
{
PageIndex = Convert.ToInt32(Context.Request.QueryString["PageIndex"].ToString());
}
catch
{
PageIndex = 1;
}
}
int PageCount = 1;
if (this.Count % this.Length == 0L)
PageCount = Convert.ToInt32(this.Count / this.Length);
else
PageCount = Convert.ToInt32(this.Count / this.Length) + 1;
string PageStr = GetPaged(this.UrlFormat, PageIndex, PageCount);
if (this.PageLocation == "Top")
{
writer.Write(PageStr);
base.Render(writer);
}
else if (this.PageLocation == "End")
{
base.Render(writer);
writer.Write(PageStr);
}
else if (this.PageLocation == "TopandEnd")
{
writer.Write(PageStr);
base.Render(writer);
writer.Write(PageStr);
}
else
{
base.Render(writer);
writer.Write(PageStr);
}
}
private static string GetPaged(string _UrlFormat, int _PageIndex, int _PageCount)
{
int pageCount = _PageCount;//总的页数
int page = _PageIndex;//当前页号
string UrlFormat = _UrlFormat + "PageIndex=";//url地址
StringBuilder HtmlString = new StringBuilder();
#region 分页功能
HtmlString.Append("");//添加css连接
if (pageCount > 0)
{
HtmlString.Append("<div class=\"page\">");
HtmlString.Append("<ul>");
HtmlString.Append("<li class=\"long\"><a href=" + UrlFormat + "1>首页</a></li>");
if (page > 1)
{
HtmlString.Append("<li class=\"long\"><a href='" + UrlFormat + "" + Convert.ToString(page - 1) + "'>上一页</a></li>");
}
int i = 1;
if (pageCount <= 10)
{
for (; i <= pageCount; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
}
else
{
if (page <= 3)
{
for (; i <= 9; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
HtmlString.Append("<li>...</li>");
for (i = pageCount - 2; i <= pageCount; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
}
else if (page >= pageCount - 2)
{
for (; i <= 3; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
HtmlString.Append("<li>...</li>");
for (i = pageCount - 4; i <= pageCount; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
}
else if (page >= 4 && page <= 5)
{
for (; i <= page-1; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
for (i = page + 1; i < page + 3; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li>...</li>");
for (i = pageCount - 2; i <= pageCount; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
else if (page >= pageCount - 4 && page <= pageCount - 3)
{
for (; i <= 3; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li>...</li>");
for (i = page - 2; i < page; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li><font color=red><strong>" + page.ToString() + "</strong></font></li>");
for (i = page + 1; i <= pageCount; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
else
{
for (; i <= 3; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li>...</li>");
for (i = page - 2; i < page; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li><font color=red><strong>" + page.ToString() + "</strong></font></li>");
for (i = page + 1; i < page + 3; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li>...</li>");
for (i = pageCount - 2; i <= pageCount; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
}
if (page < pageCount)
{
HtmlString.Append("<li class=\"long\"><a href=" + UrlFormat + "" + Convert.ToString(page + 1) + ">下一页</a></li>");
}
HtmlString.Append("<li class=\"long\"><a href=" + UrlFormat + "" + pageCount.ToString() + ">末页</a></li>");
HtmlString.Append("</ul>");
HtmlString.Append("</div>");
}
#endregion
return HtmlString.ToString();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
namespace Ncuhome.Blog.Controls
{
public class RepeaterPaged : System.Web.UI.WebControls.Repeater
{
public RepeaterPaged()
{
}
private string _PageLocation;
/// <summary>
/// 分页码的显示地方
/// Top 顶端
/// End 底端
/// TopandEnd 上面下面都有
/// </summary>
[DefaultValue("End")]
public string PageLocation
{
get { return _PageLocation; }
set { this._PageLocation = value; }
}
private string _UrlFormat;
/// <summary>
/// 地址 example"more.aspx?id=1&"
/// </summary>
public string UrlFormat
{
get { return _UrlFormat; }
set { _UrlFormat = value; }
}
private long _Count;
/// <summary>
/// 总数
/// </summary>
public long Count
{
get { return _Count; }
set { _Count = value; }
}
private long _Length;
/// <summary>
/// 每页显示条数
/// </summary>
public long Length
{
get { return _Length; }
set { _Length = value; }
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
int PageIndex = 1;
if (string.IsNullOrEmpty(Context.Request.QueryString["PageIndex"]))
{
PageIndex = 1;
}
else
{
try
{
PageIndex = Convert.ToInt32(Context.Request.QueryString["PageIndex"].ToString());
}
catch
{
PageIndex = 1;
}
}
int PageCount = 1;
if (this.Count % this.Length == 0L)
PageCount = Convert.ToInt32(this.Count / this.Length);
else
PageCount = Convert.ToInt32(this.Count / this.Length) + 1;
string PageStr = GetPaged(this.UrlFormat, PageIndex, PageCount);
if (this.PageLocation == "Top")
{
writer.Write(PageStr);
base.Render(writer);
}
else if (this.PageLocation == "End")
{
base.Render(writer);
writer.Write(PageStr);
}
else if (this.PageLocation == "TopandEnd")
{
writer.Write(PageStr);
base.Render(writer);
writer.Write(PageStr);
}
else
{
base.Render(writer);
writer.Write(PageStr);
}
}
private static string GetPaged(string _UrlFormat, int _PageIndex, int _PageCount)
{
int pageCount = _PageCount;//总的页数
int page = _PageIndex;//当前页号
string UrlFormat = _UrlFormat + "PageIndex=";//url地址
StringBuilder HtmlString = new StringBuilder();
#region 分页功能
HtmlString.Append("");//添加css连接
if (pageCount > 0)
{
HtmlString.Append("<div class=\"page\">");
HtmlString.Append("<ul>");
HtmlString.Append("<li class=\"long\"><a href=" + UrlFormat + "1>首页</a></li>");
if (page > 1)
{
HtmlString.Append("<li class=\"long\"><a href='" + UrlFormat + "" + Convert.ToString(page - 1) + "'>上一页</a></li>");
}
int i = 1;
if (pageCount <= 10)
{
for (; i <= pageCount; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
}
else
{
if (page <= 3)
{
for (; i <= 9; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
HtmlString.Append("<li>...</li>");
for (i = pageCount - 2; i <= pageCount; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
}
else if (page >= pageCount - 2)
{
for (; i <= 3; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
HtmlString.Append("<li>...</li>");
for (i = pageCount - 4; i <= pageCount; i++)
{
if (i == page)
{
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
}
else
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
}
else if (page >= 4 && page <= 5)
{
for (; i <= page-1; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li><font color=red><strong>" + i.ToString() + "</strong></font></li>");
for (i = page + 1; i < page + 3; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li>...</li>");
for (i = pageCount - 2; i <= pageCount; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
else if (page >= pageCount - 4 && page <= pageCount - 3)
{
for (; i <= 3; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li>...</li>");
for (i = page - 2; i < page; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li><font color=red><strong>" + page.ToString() + "</strong></font></li>");
for (i = page + 1; i <= pageCount; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
else
{
for (; i <= 3; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li>...</li>");
for (i = page - 2; i < page; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li><font color=red><strong>" + page.ToString() + "</strong></font></li>");
for (i = page + 1; i < page + 3; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
HtmlString.Append("<li>...</li>");
for (i = pageCount - 2; i <= pageCount; i++)
{
HtmlString.Append("<li><a href=" + UrlFormat + "" + i.ToString() + ">" + i.ToString() + "</a></li>");
}
}
}
if (page < pageCount)
{
HtmlString.Append("<li class=\"long\"><a href=" + UrlFormat + "" + Convert.ToString(page + 1) + ">下一页</a></li>");
}
HtmlString.Append("<li class=\"long\"><a href=" + UrlFormat + "" + pageCount.ToString() + ">末页</a></li>");
HtmlString.Append("</ul>");
HtmlString.Append("</div>");
}
#endregion
return HtmlString.ToString();
}
}
}
调用方式示例:
repeater1.Length = 8;
repeater1.UrlFormat = "/Log.aspx?domain=" + blogContext.Domain + "&";
repeater1.Count = Convert.ToInt64(blogContext.Owner.User_LogCount);
repeater1.DataSource = BlogCache.GetUserBlogListByBlogIdPaged(blogContext.PageIndex, 8, blogContext.BlogUserId);
repeater1.UrlFormat = "/Log.aspx?domain=" + blogContext.Domain + "&";
repeater1.Count = Convert.ToInt64(blogContext.Owner.User_LogCount);
repeater1.DataSource = BlogCache.GetUserBlogListByBlogIdPaged(blogContext.PageIndex, 8, blogContext.BlogUserId);
对应的css代码:
代码
.page{ clear:both; margin:15px 0;}
.page li,.page li.long{color: #000; font-weight:bold;width:20px;border:1px solid #DFE2E7; float: left;text-align:center; height:20px;line-height:20px;margin:1px;}
.page li a,.page li.long a{ display:block;color: #0000cc;font-weight:normal; text-decoration:none; text-align:center; height:20px;line-height:20px; width:100%;}
.page li.long,.page li.long a{ width:50px; }
.page li a:hover,.page li.long a:hover{background:#DFE2E7;}
.page li,.page li.long{color: #000; font-weight:bold;width:20px;border:1px solid #DFE2E7; float: left;text-align:center; height:20px;line-height:20px;margin:1px;}
.page li a,.page li.long a{ display:block;color: #0000cc;font-weight:normal; text-decoration:none; text-align:center; height:20px;line-height:20px; width:100%;}
.page li.long,.page li.long a{ width:50px; }
.page li a:hover,.page li.long a:hover{background:#DFE2E7;}
这几个参数需要设置没有给默认值, 需要手动赋值否则会出错。还是上大一那会写的,有些乱,也不太灵活,html和css都是固定死的。后来做ajax,翻页的时候是不能有刷新的,也有把上面的那个改成了js的写法,需要的可以看一下:javascript 实现ajax调用下的动态分页脚本
附: 通用分页
如果不想要Repeater版本,下面还有一个通用版本:
效果图片:
这个分页的样式(html和css)都是自定义的,和代码无关,你可是自定义html内容,也没有和repeater绑定
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommonPager
{
public interface IUrlManager
{
int CurrentPageIndex { get; } // 当前页码
string GetPageUrl(int pageIndex); // 根据 页码 获取页面路径
int PageCount { get; } // 总页数
int RecordCount { get; } // 记录总数
int PageSize { get; } // 分页大小
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommonPager
{
public interface IUrlManager
{
int CurrentPageIndex { get; } // 当前页码
string GetPageUrl(int pageIndex); // 根据 页码 获取页面路径
int PageCount { get; } // 总页数
int RecordCount { get; } // 记录总数
int PageSize { get; } // 分页大小
}
}
代码
using System;
using System.Web;
using System.Text.RegularExpressions;
namespace CommonPager
{
public class DefaultUrlManager : IUrlManager
{
private HttpContext context;
private Regex reg;
private string queryParam; // QueryString 参数名称
private int currentPageIndex; // 当前页
private int pageCount; // 总页数
private int recordCount; // 记录总数
private int pageSize; // 分页大小
// 构造函数
public DefaultUrlManager(int recordCount, int pageSize, string queryParam)
{
if (recordCount < 0)
throw new ArgumentOutOfRangeException("recordCount 应该大于等于 0 !");
if (pageSize <= 0)
throw new ArgumentOutOfRangeException("pageSize 应该大于 0 !");
if (string.IsNullOrEmpty(queryParam))
throw new ArgumentNullException("queryParam 不能为空!");
// 设置私有变量
this.recordCount = recordCount;
this.pageSize = pageSize;
this.queryParam = queryParam;
context = HttpContext.Current;
string pattern = @"(?<r1>[?&]" + queryParam + @"=)[^&]*";
reg = new Regex(pattern, RegexOptions.IgnoreCase);
// 如果记录数为0,至少也显示一页
if (recordCount == 0)
{
currentPageIndex = 1;
pageCount = 1;
}
else
{
// 设置总页数
double recordCount2 = Convert.ToDouble(recordCount);
double pageSize2 = Convert.ToDouble(pageSize);
pageCount = Convert.ToInt32(Math.Ceiling(recordCount2 / pageSize2));
// 设置当前页码
string queryIndex = context.Request.QueryString[queryParam];
if (string.IsNullOrEmpty(queryIndex))
currentPageIndex = 1;
else
{
try
{
currentPageIndex = Math.Abs(int.Parse(queryIndex));
if (currentPageIndex == 0)
currentPageIndex = 1;
if (currentPageIndex > pageCount)
currentPageIndex = pageCount;
}
catch
{
currentPageIndex = 1;
}
}
}
}
// 默认以 "Page" 作为 QueryString 的参数
public DefaultUrlManager(int recordCount, int pageSize) : this(recordCount, pageSize, "Page") { }
// 默认以 10 作为 PageSize
public DefaultUrlManager(int recordCount, string queryParam) : this(recordCount, 10, queryParam) { }
// 默认以 "Page" 作为 QueryString 的参数,以 10 作为分页大小
public DefaultUrlManager(int recordCount) : this(recordCount, 10) { }
public string GetPageUrl(int pageIndex)
{
string pageUrl = context.Request.Url.PathAndQuery;
// 如果找到匹配,也就是URL中含有类似 ?page=3 或者 &page=4 这样的字符串
// 则对后面的数值进行替换
if (reg.IsMatch(pageUrl))
{
pageUrl = reg.Replace(pageUrl, "${r1}" + pageIndex.ToString());
}
else
{
string queryString = context.Request.Url.Query;
if (string.IsNullOrEmpty(queryString))
pageUrl += "?" + queryParam + "=" + pageIndex.ToString();
else
pageUrl += "&" + queryParam + "=" + pageIndex.ToString();
}
return pageUrl;
}
public int CurrentPageIndex
{
get { return currentPageIndex; }
}
public int PageCount
{
get { return pageCount; }
}
public int RecordCount
{
get { return recordCount; }
}
public int PageSize
{
get { return pageSize; }
}
}
}
using System.Web;
using System.Text.RegularExpressions;
namespace CommonPager
{
public class DefaultUrlManager : IUrlManager
{
private HttpContext context;
private Regex reg;
private string queryParam; // QueryString 参数名称
private int currentPageIndex; // 当前页
private int pageCount; // 总页数
private int recordCount; // 记录总数
private int pageSize; // 分页大小
// 构造函数
public DefaultUrlManager(int recordCount, int pageSize, string queryParam)
{
if (recordCount < 0)
throw new ArgumentOutOfRangeException("recordCount 应该大于等于 0 !");
if (pageSize <= 0)
throw new ArgumentOutOfRangeException("pageSize 应该大于 0 !");
if (string.IsNullOrEmpty(queryParam))
throw new ArgumentNullException("queryParam 不能为空!");
// 设置私有变量
this.recordCount = recordCount;
this.pageSize = pageSize;
this.queryParam = queryParam;
context = HttpContext.Current;
string pattern = @"(?<r1>[?&]" + queryParam + @"=)[^&]*";
reg = new Regex(pattern, RegexOptions.IgnoreCase);
// 如果记录数为0,至少也显示一页
if (recordCount == 0)
{
currentPageIndex = 1;
pageCount = 1;
}
else
{
// 设置总页数
double recordCount2 = Convert.ToDouble(recordCount);
double pageSize2 = Convert.ToDouble(pageSize);
pageCount = Convert.ToInt32(Math.Ceiling(recordCount2 / pageSize2));
// 设置当前页码
string queryIndex = context.Request.QueryString[queryParam];
if (string.IsNullOrEmpty(queryIndex))
currentPageIndex = 1;
else
{
try
{
currentPageIndex = Math.Abs(int.Parse(queryIndex));
if (currentPageIndex == 0)
currentPageIndex = 1;
if (currentPageIndex > pageCount)
currentPageIndex = pageCount;
}
catch
{
currentPageIndex = 1;
}
}
}
}
// 默认以 "Page" 作为 QueryString 的参数
public DefaultUrlManager(int recordCount, int pageSize) : this(recordCount, pageSize, "Page") { }
// 默认以 10 作为 PageSize
public DefaultUrlManager(int recordCount, string queryParam) : this(recordCount, 10, queryParam) { }
// 默认以 "Page" 作为 QueryString 的参数,以 10 作为分页大小
public DefaultUrlManager(int recordCount) : this(recordCount, 10) { }
public string GetPageUrl(int pageIndex)
{
string pageUrl = context.Request.Url.PathAndQuery;
// 如果找到匹配,也就是URL中含有类似 ?page=3 或者 &page=4 这样的字符串
// 则对后面的数值进行替换
if (reg.IsMatch(pageUrl))
{
pageUrl = reg.Replace(pageUrl, "${r1}" + pageIndex.ToString());
}
else
{
string queryString = context.Request.Url.Query;
if (string.IsNullOrEmpty(queryString))
pageUrl += "?" + queryParam + "=" + pageIndex.ToString();
else
pageUrl += "&" + queryParam + "=" + pageIndex.ToString();
}
return pageUrl;
}
public int CurrentPageIndex
{
get { return currentPageIndex; }
}
public int PageCount
{
get { return pageCount; }
}
public int RecordCount
{
get { return recordCount; }
}
public int PageSize
{
get { return pageSize; }
}
}
}
代码
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CommonPager
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Pager runat=server ></{0}:Pager>")]
public class Pager : WebControl
{
#region Field
private int currentPage; // 当前页的页码
private int previousPageCount = 5; // 当前页之前可以显示的最多条目数,大于此条目的将被隐藏
private int afterPageCount = 4; // 当前页之后可以显示的最多条目数,大于此条目的将被隐藏
private int pageCount; // 总页数
private bool showPrevious = false; // 是否显示 上一页、第一页 的链接
private bool showNext = false; // 是否显示 下一页、最末页 的链接
private int startPage; // 显示的第一页 的 页码
private int endPage; // 显示的最末页 的 页码
private string currentPageFormat = @"<a href='{0}' class='Current'>{1}</a>";
private string normalPageFormat = @"<a href='{0}' class='PageIcon'>{1}</a>";
private string headFormat = @"";
private string footerFormat = @"";
private string previousPageFormat = @"<a href='{0}' class='PageIcon'>上一页</a>";
private string nextPageFormat = @"<a href='{0}' class='PageIcon'>下一页</a>";
private string firstPageFormat = @"<a href='{0}' class='PageIcon'>第一页</a>";
private string lastPageFormat = @"<a href='{0}' class='PageIcon'>最末页</a>";
private string statPageFormat = @" ( 第<b>{0}</b>页/共<b>{1}</b>页 )";
private IUrlManager urlManager; // 设置接口
#endregion
#region Property]
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string HeadFormat
{
get {
return headFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The HeaderFormat can not be null");
}
headFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string FooterFormat
{
get { return footerFormat; }
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The FooterFormat can not be null");
}
footerFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string PreviousPageFormat
{
get
{
return previousPageFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The PreviousPageFormat can not be null");
}
previousPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string NextPageFormat
{
get
{
return nextPageFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The NextPageFormat can not be null");
}
nextPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string StatPageFormat
{
get {
return statPageFormat;
}
set {
if (string.IsNullOrEmpty(value))
{
throw new Exception("The StatPageFormat can not be null");
}
statPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string FirstPageFormat
{
get { return firstPageFormat; }
set {
if (string.IsNullOrEmpty(value))
{
throw new Exception("The FirstPageFormat can not be null");
}
firstPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string LastPageFormat
{
get { return lastPageFormat; }
set {
if (string.IsNullOrEmpty(value))
{
throw new Exception("The LastPageFormat can not be null");
}
lastPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string NormalPageStringFormat
{
get {
return normalPageFormat;
}
set {
if (string.IsNullOrEmpty(value))
{
throw new Exception("The NormalPageFormat can not be null");
}
normalPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string CurrentPageStringFormat
{
get
{
return currentPageFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The CurrentPageFormat can not be null");
}
currentPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public int PreviousPageCount
{
get { return previousPageCount; }
set { previousPageCount = value; }
}
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public int AfterPageCount
{
get { return afterPageCount; }
set { afterPageCount = value; }
}
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public IUrlManager UrlManager
{
get { return urlManager; }
set { urlManager = value; }
}
#endregion
#region Method
protected override void RenderContents(HtmlTextWriter output)
{
if (urlManager == null)
throw new ArgumentNullException("IUrlManager 不能为 Null");
// 获取当前页
currentPage = urlManager.CurrentPageIndex;
// 获取总页数
pageCount = urlManager.PageCount;
SetStartPage();
SetEndPage();
HyperLink link;
output.Write(HeadFormat);
// 循环打印链接
for (int i = startPage; i <= endPage; i++)
{
if (showPrevious) // 如果需要显示前一页、第一页链接
AddPreviousLink(urlManager, output);
link = new HyperLink();
if (i == currentPage)
{
this.MakeLink(output, CurrentPageStringFormat, i, urlManager.GetPageUrl(i));
//this.MakeLink(output, "CurrentPage", "第" + i.ToString() + "页", urlManager.GetPageUrl(i));
}
else
{
this.MakeLink(output, NormalPageStringFormat, i, urlManager.GetPageUrl(i));
//this.MakeLink(output, "PagerIcon", "第" + i.ToString() + "页", urlManager.GetPageUrl(i));
}
if (i == endPage && showNext) // 如果需要显示 下一页、最末页 链接
AddNextLink(urlManager, output);
}
output.Write(String.Format(StatPageFormat, currentPage, pageCount));
output.Write(FooterFormat);
}
// 添加“第一页”,“上一页”的连接
private void AddPreviousLink(IUrlManager urlManager, HtmlTextWriter output)
{
output.Write(String.Format(FirstPageFormat, urlManager.GetPageUrl(1)));
output.Write(String.Format(PreviousPageFormat, urlManager.GetPageUrl(currentPage - 1)));
//this.MakeLink(output, "PagerIcon", "第一页", urlManager.GetPageUrl(1));
//this.MakeLink(output, "PagerIcon", "上一页", urlManager.GetPageUrl(currentPage - 1));
showPrevious = false; // 只显示一次
}
// 添加 “下一页”、“最末页” 的链接
private void AddNextLink(IUrlManager urlManager, HtmlTextWriter output)
{
output.Write(String.Format(NextPageFormat, urlManager.GetPageUrl(currentPage + 1)));
output.Write(String.Format(LastPageFormat, urlManager.GetPageUrl(pageCount)));
//this.MakeLink(output, "PagerIcon", "下一页", urlManager.GetPageUrl(currentPage + 1));
//this.MakeLink(output, "PagerIcon", "最末页", urlManager.GetPageUrl(pageCount));
showNext = false; // 可有可无,程序会跳出循环
}
// 根据当前页,当前页之前可以显示的页数,算得从第几页开始进行显示
private void SetStartPage()
{
// 如果当前页小于 它前面所可以显示的条目数,那么显示第一页就是实际的第一页
if (currentPage <= previousPageCount)
{
startPage = 1;
}
else // 这种情况下 currentPage 前面总是能显示完,要根据后面的长短确定是不是前面应该多显示
{
if (currentPage > previousPageCount + 1)
showPrevious = true;
int linkLength = (pageCount - currentPage + 1) + previousPageCount;
int startPage = currentPage - previousPageCount;
while (linkLength < previousPageCount + afterPageCount + 1 && startPage > 1)
{
linkLength++;
startPage--;
}
this.startPage = startPage;
}
}
// 根据CurrentPage、总页数、当前页之后长度 算得显示的最末页是 第几页
private void SetEndPage()
{
// 如果当前页加上它之后可以显示的页数 大于 总页数,那么显示的最末页就是实际的最末页
if (currentPage + afterPageCount >= pageCount)
{
endPage = pageCount;
}
else // 这种情况下 currentPage后面的总是可以显示完,要根据前面的长短确定是不是后面应该多显示
{
int linkLength = (currentPage - startPage + 1) + afterPageCount;
int endPage = currentPage + afterPageCount;
while (linkLength < previousPageCount + afterPageCount + 1 && endPage < pageCount)
{
linkLength++;
endPage++;
}
if (endPage < pageCount)
showNext = true;
this.endPage = endPage;
}
}
/// <summary>
///
/// </summary>
/// <param name="output"></param>
/// <param name="Class"></param>
/// <param name="Title"></param>
/// <param name="Href"></param>
private void MakeLink(HtmlTextWriter output,string Class,string Title,string Href)
{
output.AddAttribute(HtmlTextWriterAttribute.Href, Href);
output.AddAttribute(HtmlTextWriterAttribute.Class, Class);
output.AddAttribute(HtmlTextWriterAttribute.Title, Title);
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write(Title);
output.WriteEndTag("a");
}
private void MakeLink(HtmlTextWriter output, string format, int pageNumer,string href)
{
output.Write(string.Format(format, href, pageNumer));
}
#endregion
}
}
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CommonPager
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:Pager runat=server ></{0}:Pager>")]
public class Pager : WebControl
{
#region Field
private int currentPage; // 当前页的页码
private int previousPageCount = 5; // 当前页之前可以显示的最多条目数,大于此条目的将被隐藏
private int afterPageCount = 4; // 当前页之后可以显示的最多条目数,大于此条目的将被隐藏
private int pageCount; // 总页数
private bool showPrevious = false; // 是否显示 上一页、第一页 的链接
private bool showNext = false; // 是否显示 下一页、最末页 的链接
private int startPage; // 显示的第一页 的 页码
private int endPage; // 显示的最末页 的 页码
private string currentPageFormat = @"<a href='{0}' class='Current'>{1}</a>";
private string normalPageFormat = @"<a href='{0}' class='PageIcon'>{1}</a>";
private string headFormat = @"";
private string footerFormat = @"";
private string previousPageFormat = @"<a href='{0}' class='PageIcon'>上一页</a>";
private string nextPageFormat = @"<a href='{0}' class='PageIcon'>下一页</a>";
private string firstPageFormat = @"<a href='{0}' class='PageIcon'>第一页</a>";
private string lastPageFormat = @"<a href='{0}' class='PageIcon'>最末页</a>";
private string statPageFormat = @" ( 第<b>{0}</b>页/共<b>{1}</b>页 )";
private IUrlManager urlManager; // 设置接口
#endregion
#region Property]
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string HeadFormat
{
get {
return headFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The HeaderFormat can not be null");
}
headFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string FooterFormat
{
get { return footerFormat; }
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The FooterFormat can not be null");
}
footerFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string PreviousPageFormat
{
get
{
return previousPageFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The PreviousPageFormat can not be null");
}
previousPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string NextPageFormat
{
get
{
return nextPageFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The NextPageFormat can not be null");
}
nextPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string StatPageFormat
{
get {
return statPageFormat;
}
set {
if (string.IsNullOrEmpty(value))
{
throw new Exception("The StatPageFormat can not be null");
}
statPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string FirstPageFormat
{
get { return firstPageFormat; }
set {
if (string.IsNullOrEmpty(value))
{
throw new Exception("The FirstPageFormat can not be null");
}
firstPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string LastPageFormat
{
get { return lastPageFormat; }
set {
if (string.IsNullOrEmpty(value))
{
throw new Exception("The LastPageFormat can not be null");
}
lastPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string NormalPageStringFormat
{
get {
return normalPageFormat;
}
set {
if (string.IsNullOrEmpty(value))
{
throw new Exception("The NormalPageFormat can not be null");
}
normalPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string CurrentPageStringFormat
{
get
{
return currentPageFormat;
}
set
{
if (string.IsNullOrEmpty(value))
{
throw new Exception("The CurrentPageFormat can not be null");
}
currentPageFormat = value;
}
}
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string Text
{
get
{
String s = (String)ViewState["Text"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["Text"] = value;
}
}
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public int PreviousPageCount
{
get { return previousPageCount; }
set { previousPageCount = value; }
}
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public int AfterPageCount
{
get { return afterPageCount; }
set { afterPageCount = value; }
}
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public IUrlManager UrlManager
{
get { return urlManager; }
set { urlManager = value; }
}
#endregion
#region Method
protected override void RenderContents(HtmlTextWriter output)
{
if (urlManager == null)
throw new ArgumentNullException("IUrlManager 不能为 Null");
// 获取当前页
currentPage = urlManager.CurrentPageIndex;
// 获取总页数
pageCount = urlManager.PageCount;
SetStartPage();
SetEndPage();
HyperLink link;
output.Write(HeadFormat);
// 循环打印链接
for (int i = startPage; i <= endPage; i++)
{
if (showPrevious) // 如果需要显示前一页、第一页链接
AddPreviousLink(urlManager, output);
link = new HyperLink();
if (i == currentPage)
{
this.MakeLink(output, CurrentPageStringFormat, i, urlManager.GetPageUrl(i));
//this.MakeLink(output, "CurrentPage", "第" + i.ToString() + "页", urlManager.GetPageUrl(i));
}
else
{
this.MakeLink(output, NormalPageStringFormat, i, urlManager.GetPageUrl(i));
//this.MakeLink(output, "PagerIcon", "第" + i.ToString() + "页", urlManager.GetPageUrl(i));
}
if (i == endPage && showNext) // 如果需要显示 下一页、最末页 链接
AddNextLink(urlManager, output);
}
output.Write(String.Format(StatPageFormat, currentPage, pageCount));
output.Write(FooterFormat);
}
// 添加“第一页”,“上一页”的连接
private void AddPreviousLink(IUrlManager urlManager, HtmlTextWriter output)
{
output.Write(String.Format(FirstPageFormat, urlManager.GetPageUrl(1)));
output.Write(String.Format(PreviousPageFormat, urlManager.GetPageUrl(currentPage - 1)));
//this.MakeLink(output, "PagerIcon", "第一页", urlManager.GetPageUrl(1));
//this.MakeLink(output, "PagerIcon", "上一页", urlManager.GetPageUrl(currentPage - 1));
showPrevious = false; // 只显示一次
}
// 添加 “下一页”、“最末页” 的链接
private void AddNextLink(IUrlManager urlManager, HtmlTextWriter output)
{
output.Write(String.Format(NextPageFormat, urlManager.GetPageUrl(currentPage + 1)));
output.Write(String.Format(LastPageFormat, urlManager.GetPageUrl(pageCount)));
//this.MakeLink(output, "PagerIcon", "下一页", urlManager.GetPageUrl(currentPage + 1));
//this.MakeLink(output, "PagerIcon", "最末页", urlManager.GetPageUrl(pageCount));
showNext = false; // 可有可无,程序会跳出循环
}
// 根据当前页,当前页之前可以显示的页数,算得从第几页开始进行显示
private void SetStartPage()
{
// 如果当前页小于 它前面所可以显示的条目数,那么显示第一页就是实际的第一页
if (currentPage <= previousPageCount)
{
startPage = 1;
}
else // 这种情况下 currentPage 前面总是能显示完,要根据后面的长短确定是不是前面应该多显示
{
if (currentPage > previousPageCount + 1)
showPrevious = true;
int linkLength = (pageCount - currentPage + 1) + previousPageCount;
int startPage = currentPage - previousPageCount;
while (linkLength < previousPageCount + afterPageCount + 1 && startPage > 1)
{
linkLength++;
startPage--;
}
this.startPage = startPage;
}
}
// 根据CurrentPage、总页数、当前页之后长度 算得显示的最末页是 第几页
private void SetEndPage()
{
// 如果当前页加上它之后可以显示的页数 大于 总页数,那么显示的最末页就是实际的最末页
if (currentPage + afterPageCount >= pageCount)
{
endPage = pageCount;
}
else // 这种情况下 currentPage后面的总是可以显示完,要根据前面的长短确定是不是后面应该多显示
{
int linkLength = (currentPage - startPage + 1) + afterPageCount;
int endPage = currentPage + afterPageCount;
while (linkLength < previousPageCount + afterPageCount + 1 && endPage < pageCount)
{
linkLength++;
endPage++;
}
if (endPage < pageCount)
showNext = true;
this.endPage = endPage;
}
}
/// <summary>
///
/// </summary>
/// <param name="output"></param>
/// <param name="Class"></param>
/// <param name="Title"></param>
/// <param name="Href"></param>
private void MakeLink(HtmlTextWriter output,string Class,string Title,string Href)
{
output.AddAttribute(HtmlTextWriterAttribute.Href, Href);
output.AddAttribute(HtmlTextWriterAttribute.Class, Class);
output.AddAttribute(HtmlTextWriterAttribute.Title, Title);
output.RenderBeginTag(HtmlTextWriterTag.A);
output.Write(Title);
output.WriteEndTag("a");
}
private void MakeLink(HtmlTextWriter output, string format, int pageNumer,string href)
{
output.Write(string.Format(format, href, pageNumer));
}
#endregion
}
}
调用方式:
代码
pager1.CurrentPageStringFormat = "<span class=\"current\">{1}</span>";
pager1.StatPageFormat = " ";
pager1.LastPageFormat = " ";
CommonPager.DefaultUrlManager cp = new CommonPager.DefaultUrlManager(20000,20, "pageindex");
pager1.UrlManager = cp;
pager1.CurrentPageStringFormat = "<span class=\"current\">{1}</span>";
pager1.StatPageFormat = " ";
pager1.LastPageFormat = " ";
CommonPager.DefaultUrlManager cp = new CommonPager.DefaultUrlManager(20000,20, "pageindex");
pager1.UrlManager = cp;