利用QueryString实现分页参数传递的分页条控件, 纯UI层使用, 不影响分层, 简单实用!
主要属性: RecordCount
设计时图示:
源代码:
using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[assembly: TagPrefix("EFPlatform.WebControls", "efp")]
namespace EFPlatform.WebControls
{
[ToolboxData("<{0}:PagerBar runat="server"></{0}:PagerBar>")]
public class PagerBar : Control, INamingContainer
{
#region Properties
#region Data(数据)
#region CurrentPage
[Category("Data")]
[DefaultValue(1)]
public int CurrentPage
{
get
{
if(ViewState["CurrentPage"] == null)
{
try
{
ViewState["CurrentPage"] = int.Parse(HttpContext.Current.Request.QueryString[PageParamName]);
}
catch
{
ViewState["CurrentPage"] = 1;
}
}
return (int)ViewState["CurrentPage"];
}
set
{
ViewState["CurrentPage"] = value;
}
}
#endregion
#region PageSize
[Category("Data")]
[DefaultValue(10)]
public int PageSize
{
get
{
int pageSize = (ViewState["PageSize"] == null) ? 10 : (int)ViewState["PageSize"];
return (pageSize > 0) ? pageSize : 0;
}
set
{
ViewState["PageSize"] = value;
}
}
#endregion
#region RecordCount
[Category("Data")]
[DefaultValue(0)]
public int RecordCount
{
get
{
return (ViewState["RecordCount"] == null) ? 0 : (int)ViewState["RecordCount"];
}
set
{
ViewState["RecordCount"] = value;
}
}
#endregion
#region PageCount
[Browsable(false)]
public int PageCount
{
get
{
return (int)Math.Ceiling((double)RecordCount / PageSize);
}
}
#endregion
#endregion
#region Navigation(导航)
#region PageParamName
[Category("Navigation")]
[DefaultValue("CurrentPage")]
public string PageParamName
{
get
{
return (ViewState["PageParamName"] == null) ? "CurrentPage" : (string)ViewState["PageParamName"];
}
set
{
ViewState["PageParamName"] = value;
}
}
#endregion
#region FirstPageText
[Category("Navigation")]
[DefaultValue("首页")]
public string FirstPageText
{
get
{
return (ViewState["FirstPageText"] == null) ? "首页" : (string)ViewState["FirstPageText"];
}
set
{
ViewState["FirstPageText"] = value;
}
}
#endregion
#region PrePageText
[Category("Navigation")]
[DefaultValue("上一页")]
public string PrePageText
{
get
{
return (ViewState["PrePageText"] == null) ? "上一页" : (string)ViewState["PrePageText"];
}
set
{
ViewState["PrePageText"] = value;
}
}
#endregion
#region NextPageText
[Category("Navigation")]
[DefaultValue("下一页")]
public string NextPageText
{
get
{
return (ViewState["NextPageText"] == null) ? "下一页" : (string)ViewState["NextPageText"];
}
set
{
ViewState["NextPageText"] = value;
}
}
#endregion
#region LastPageText
[Category("Navigation")]
[DefaultValue("末页")]
public string LastPageText
{
get
{
return (ViewState["LastPageText"] == null) ? "末页" : (string)ViewState["LastPageText"];
}
set
{
ViewState["LastPageText"] = value;
}
}
#endregion
#endregion
#region Layout(布局)
#region Width
[Category("Layout")]
public Unit Width
{
get
{
return (ViewState["Width"] == null) ? Unit.Percentage(100) : (Unit)ViewState["Width"];
}
set
{
ViewState["Width"] = value;
}
}
#endregion
#region Height
[Category("Layout")]
public Unit Height
{
get
{
return (ViewState["Height"] == null) ? Unit.Pixel(20) : (Unit)ViewState["Height"];
}
set
{
ViewState["Height"] = value;
}
}
#endregion
#region CellPadding
[Category("Layout")]
[DefaultValue(0)]
public int CellPadding
{
get
{
return (ViewState["CellPadding"] == null) ? 0 : (int)ViewState["CellPadding"];
}
set
{
ViewState["CellPadding"] = value;
}
}
#endregion
#region CellSpacing
[Category("Layout")]
[DefaultValue(0)]
public int CellSpacing
{
get
{
return (ViewState["CellSpacing"] == null) ? 0 : (int)ViewState["CellSpacing"];
}
set
{
ViewState["CellSpacing"] = value;
}
}
#endregion
#endregion
#region Appearance(外观)
#region TableCssClass
[Category("Appearance")]
public string TableCssClass
{
get
{
return (ViewState["TableCssClass"] == null) ? string.Empty : (string)ViewState["TableCssClass"];
}
set
{
ViewState["TableCssClass"] = value;
}
}
#endregion
#region TableCellCssClass
[Category("Appearance")]
public string TableCellCssClass
{
get
{
return (ViewState["TableCellCssClass"] == null) ? string.Empty : (string)ViewState["TableCellCssClass"];
}
set
{
ViewState["TableCellCssClass"] = value;
}
}
#endregion
#region PagerCssClass
[Category("Appearance")]
public string PagerCssClass
{
get
{
return (ViewState["PagerCssClass"] == null) ? string.Empty : (string)ViewState["PagerCssClass"];
}
set
{
ViewState["PagerCssClass"] = value;
}
}
#endregion
#endregion
#endregion
#region GetPagerUrl
private string GetPagerUrl()
{
StringBuilder sbUrl = new StringBuilder(HttpContext.Current.Request.Path);
sbUrl.Append('?');
NameValueCollection query = HttpContext.Current.Request.QueryString;
for(int i = 0; i < query.Count; i++)
{
if(query.GetKey(i) != PageParamName)
{
sbUrl.Append(query.GetKey(i));
sbUrl.Append('=');
sbUrl.Append(HttpContext.Current.Server.UrlEncode(query.Get(i)));
sbUrl.Append('&');
}
}
sbUrl.Append(PageParamName);
sbUrl.Append('=');
return sbUrl.ToString();
}
#endregion
#region GetPagerLinks
private string[] GetPagerLinks()
{
string[] links = new string[5];
int prePage = 1;
int nextPage = 1;
if(RecordCount > 0)
{
string pagerUrl = this.GetPagerUrl();
prePage = (CurrentPage > 1) ? (CurrentPage - 1) : 1;
nextPage = (CurrentPage + 1 > PageCount) ? PageCount : CurrentPage + 1;
links[0] = pagerUrl + "1";
links[1] = pagerUrl + prePage.ToString();
links[2] = pagerUrl + nextPage.ToString();
links[3] = pagerUrl + PageCount.ToString();
links[4] = pagerUrl;
return links;
}
else
{
CurrentPage = 0;
}
return links;
}
#endregion
#region GetPagerOptions
private string GetPagerOptions()
{
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= this.PageCount; i++)
{
if(i == this.CurrentPage)
{
sb.AppendFormat("<OPTION value="{0}" selected>{0}</OPTION>", i);
}
else
{
sb.AppendFormat("<OPTION value="{0}">{0}</OPTION>", i);
}
}
return sb.ToString();
}
#endregion
#region GetHTML
private string GetHTML()
{
string[] links = this.GetPagerLinks();
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<TABLE width="{0}%" height="{1}" cellpadding="{2}" cellspacing="{3}" class="{4}"><TR>", this.Width.Value, this.Height.Value, this.CellPadding, this.CellSpacing, this.TableCssClass);
sb.AppendFormat("<TD align="left" width="100" class="{0}">", this.TableCellCssClass);
sb.AppendFormat("<SPAN>共 {0} 条数据</SPAN>", this.RecordCount);
sb.AppendFormat("</TD><TD align="center" class="{0}">", this.TableCellCssClass);
sb.AppendFormat("<A class="{0}" href="{1}">{2}</A> ", this.PagerCssClass, links[0], this.FirstPageText);
sb.AppendFormat("<A class="{0}" href="{1}">{2}</A> ", this.PagerCssClass, links[1], this.PrePageText);
sb.AppendFormat("<A class="{0}" href="{1}">{2}</A> ", this.PagerCssClass, links[2], this.NextPageText);
sb.AppendFormat("<A class="{0}" href="{1}">{2}</A> ", this.PagerCssClass, links[3], this.LastPageText);
sb.AppendFormat(" 转到<SELECT onchange="window.location.href='{0}'+this.options[this.selectedIndex].value;">{1}</SELECT>页", links[4], this.GetPagerOptions());
sb.AppendFormat("</TD><TD align="right" class="{0}">第 {1} / {2} 页</TD>", this.TableCellCssClass, this.CurrentPage, this.PageCount);
sb.AppendFormat("</TR></TABLE>");
return sb.ToString();
}
#endregion
#region override Render
protected override void Render(HtmlTextWriter writer)
{
writer.Write(this.GetHTML());
base.Render (writer);
}
#endregion
}
}
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
[assembly: TagPrefix("EFPlatform.WebControls", "efp")]
namespace EFPlatform.WebControls
{
[ToolboxData("<{0}:PagerBar runat="server"></{0}:PagerBar>")]
public class PagerBar : Control, INamingContainer
{
#region Properties
#region Data(数据)
#region CurrentPage
[Category("Data")]
[DefaultValue(1)]
public int CurrentPage
{
get
{
if(ViewState["CurrentPage"] == null)
{
try
{
ViewState["CurrentPage"] = int.Parse(HttpContext.Current.Request.QueryString[PageParamName]);
}
catch
{
ViewState["CurrentPage"] = 1;
}
}
return (int)ViewState["CurrentPage"];
}
set
{
ViewState["CurrentPage"] = value;
}
}
#endregion
#region PageSize
[Category("Data")]
[DefaultValue(10)]
public int PageSize
{
get
{
int pageSize = (ViewState["PageSize"] == null) ? 10 : (int)ViewState["PageSize"];
return (pageSize > 0) ? pageSize : 0;
}
set
{
ViewState["PageSize"] = value;
}
}
#endregion
#region RecordCount
[Category("Data")]
[DefaultValue(0)]
public int RecordCount
{
get
{
return (ViewState["RecordCount"] == null) ? 0 : (int)ViewState["RecordCount"];
}
set
{
ViewState["RecordCount"] = value;
}
}
#endregion
#region PageCount
[Browsable(false)]
public int PageCount
{
get
{
return (int)Math.Ceiling((double)RecordCount / PageSize);
}
}
#endregion
#endregion
#region Navigation(导航)
#region PageParamName
[Category("Navigation")]
[DefaultValue("CurrentPage")]
public string PageParamName
{
get
{
return (ViewState["PageParamName"] == null) ? "CurrentPage" : (string)ViewState["PageParamName"];
}
set
{
ViewState["PageParamName"] = value;
}
}
#endregion
#region FirstPageText
[Category("Navigation")]
[DefaultValue("首页")]
public string FirstPageText
{
get
{
return (ViewState["FirstPageText"] == null) ? "首页" : (string)ViewState["FirstPageText"];
}
set
{
ViewState["FirstPageText"] = value;
}
}
#endregion
#region PrePageText
[Category("Navigation")]
[DefaultValue("上一页")]
public string PrePageText
{
get
{
return (ViewState["PrePageText"] == null) ? "上一页" : (string)ViewState["PrePageText"];
}
set
{
ViewState["PrePageText"] = value;
}
}
#endregion
#region NextPageText
[Category("Navigation")]
[DefaultValue("下一页")]
public string NextPageText
{
get
{
return (ViewState["NextPageText"] == null) ? "下一页" : (string)ViewState["NextPageText"];
}
set
{
ViewState["NextPageText"] = value;
}
}
#endregion
#region LastPageText
[Category("Navigation")]
[DefaultValue("末页")]
public string LastPageText
{
get
{
return (ViewState["LastPageText"] == null) ? "末页" : (string)ViewState["LastPageText"];
}
set
{
ViewState["LastPageText"] = value;
}
}
#endregion
#endregion
#region Layout(布局)
#region Width
[Category("Layout")]
public Unit Width
{
get
{
return (ViewState["Width"] == null) ? Unit.Percentage(100) : (Unit)ViewState["Width"];
}
set
{
ViewState["Width"] = value;
}
}
#endregion
#region Height
[Category("Layout")]
public Unit Height
{
get
{
return (ViewState["Height"] == null) ? Unit.Pixel(20) : (Unit)ViewState["Height"];
}
set
{
ViewState["Height"] = value;
}
}
#endregion
#region CellPadding
[Category("Layout")]
[DefaultValue(0)]
public int CellPadding
{
get
{
return (ViewState["CellPadding"] == null) ? 0 : (int)ViewState["CellPadding"];
}
set
{
ViewState["CellPadding"] = value;
}
}
#endregion
#region CellSpacing
[Category("Layout")]
[DefaultValue(0)]
public int CellSpacing
{
get
{
return (ViewState["CellSpacing"] == null) ? 0 : (int)ViewState["CellSpacing"];
}
set
{
ViewState["CellSpacing"] = value;
}
}
#endregion
#endregion
#region Appearance(外观)
#region TableCssClass
[Category("Appearance")]
public string TableCssClass
{
get
{
return (ViewState["TableCssClass"] == null) ? string.Empty : (string)ViewState["TableCssClass"];
}
set
{
ViewState["TableCssClass"] = value;
}
}
#endregion
#region TableCellCssClass
[Category("Appearance")]
public string TableCellCssClass
{
get
{
return (ViewState["TableCellCssClass"] == null) ? string.Empty : (string)ViewState["TableCellCssClass"];
}
set
{
ViewState["TableCellCssClass"] = value;
}
}
#endregion
#region PagerCssClass
[Category("Appearance")]
public string PagerCssClass
{
get
{
return (ViewState["PagerCssClass"] == null) ? string.Empty : (string)ViewState["PagerCssClass"];
}
set
{
ViewState["PagerCssClass"] = value;
}
}
#endregion
#endregion
#endregion
#region GetPagerUrl
private string GetPagerUrl()
{
StringBuilder sbUrl = new StringBuilder(HttpContext.Current.Request.Path);
sbUrl.Append('?');
NameValueCollection query = HttpContext.Current.Request.QueryString;
for(int i = 0; i < query.Count; i++)
{
if(query.GetKey(i) != PageParamName)
{
sbUrl.Append(query.GetKey(i));
sbUrl.Append('=');
sbUrl.Append(HttpContext.Current.Server.UrlEncode(query.Get(i)));
sbUrl.Append('&');
}
}
sbUrl.Append(PageParamName);
sbUrl.Append('=');
return sbUrl.ToString();
}
#endregion
#region GetPagerLinks
private string[] GetPagerLinks()
{
string[] links = new string[5];
int prePage = 1;
int nextPage = 1;
if(RecordCount > 0)
{
string pagerUrl = this.GetPagerUrl();
prePage = (CurrentPage > 1) ? (CurrentPage - 1) : 1;
nextPage = (CurrentPage + 1 > PageCount) ? PageCount : CurrentPage + 1;
links[0] = pagerUrl + "1";
links[1] = pagerUrl + prePage.ToString();
links[2] = pagerUrl + nextPage.ToString();
links[3] = pagerUrl + PageCount.ToString();
links[4] = pagerUrl;
return links;
}
else
{
CurrentPage = 0;
}
return links;
}
#endregion
#region GetPagerOptions
private string GetPagerOptions()
{
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= this.PageCount; i++)
{
if(i == this.CurrentPage)
{
sb.AppendFormat("<OPTION value="{0}" selected>{0}</OPTION>", i);
}
else
{
sb.AppendFormat("<OPTION value="{0}">{0}</OPTION>", i);
}
}
return sb.ToString();
}
#endregion
#region GetHTML
private string GetHTML()
{
string[] links = this.GetPagerLinks();
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<TABLE width="{0}%" height="{1}" cellpadding="{2}" cellspacing="{3}" class="{4}"><TR>", this.Width.Value, this.Height.Value, this.CellPadding, this.CellSpacing, this.TableCssClass);
sb.AppendFormat("<TD align="left" width="100" class="{0}">", this.TableCellCssClass);
sb.AppendFormat("<SPAN>共 {0} 条数据</SPAN>", this.RecordCount);
sb.AppendFormat("</TD><TD align="center" class="{0}">", this.TableCellCssClass);
sb.AppendFormat("<A class="{0}" href="{1}">{2}</A> ", this.PagerCssClass, links[0], this.FirstPageText);
sb.AppendFormat("<A class="{0}" href="{1}">{2}</A> ", this.PagerCssClass, links[1], this.PrePageText);
sb.AppendFormat("<A class="{0}" href="{1}">{2}</A> ", this.PagerCssClass, links[2], this.NextPageText);
sb.AppendFormat("<A class="{0}" href="{1}">{2}</A> ", this.PagerCssClass, links[3], this.LastPageText);
sb.AppendFormat(" 转到<SELECT onchange="window.location.href='{0}'+this.options[this.selectedIndex].value;">{1}</SELECT>页", links[4], this.GetPagerOptions());
sb.AppendFormat("</TD><TD align="right" class="{0}">第 {1} / {2} 页</TD>", this.TableCellCssClass, this.CurrentPage, this.PageCount);
sb.AppendFormat("</TR></TABLE>");
return sb.ToString();
}
#endregion
#region override Render
protected override void Render(HtmlTextWriter writer)
{
writer.Write(this.GetHTML());
base.Render (writer);
}
#endregion
}
}