为什么要自己写控件页不用网上成熟的控件呢?
1:网上成熟的控件封装性太强;
2:功能太多,有些用不着;
3:不太适用自己实际应用 .
在前两篇文章中,本人试着探索自定义控件的写法,当时写的不太好,功能也少,现在在两篇文章的基础上加以整理和改造,完成了下面的控件.
功能:1:支持url参数分页;
2:运行回发事件分页;
3:可以自定义样式( 样式的名称是写死的,用户只能更改样式内容);
4:本控件图例
在页数多的时候,用户可以设置省略号前面和后面显示的数量,上图设置都是5;
5:可以设置跳转到文本框的显示与否.
说明:本人参考了AspNetPager控制的源代码主要目的是:学习自定义控件的用法,可能结合自己的实际应用来写出适合自己的控件
代码有点长,本人都写在一个文件中了,希望各位指点:
代码如下:
Code
using System;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
[assembly: WebResource("pagerControls.Resources.listPager.css", "text/css", PerformSubstitution = true)]
namespace pagerControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControlPager runat=server></{0}:WebCustomControlPager>")]
[ParseChildren(true)]
public class WebCustomControlPager : CompositeControl, INamingContainer, IPostBackEventHandler, IPostBackDataHandler
{
Properties#region Properties
private int _currentPageIndex = 1;
/**//// <summary>
/// 当前页面索引
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("当前页面索引从1开始")]
[DefaultValueAttribute("当前页面索引")]
public int currentPageIndex
{
get
{
return ((ViewState["currentPageIndex"] == null) ? this._currentPageIndex : (int)ViewState["currentPageIndex"]);
}
set
{
ViewState["currentPageIndex"] = value;
}
}
/**//// <summary>
/// 是否自定义样式
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("是否自定义样式")]
[DefaultValueAttribute("0")]
public bool IsCustomStyle
{
get
{
return ((ViewState["IsCustomStyle"] == null) ? false : (bool)ViewState["IsCustomStyle"]);
}
set
{
ViewState["IsCustomStyle"] = value;
}
}
private int _iRecordCount = 1;
/**//// <summary>
/// 记录数量
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("记录数量")]
[DefaultValueAttribute("记录数量")]
public int iRecordCount
{
set
{
ViewState["iRecordCount"] = value;
this._iRecordCount = value;
}
get
{
return ((ViewState["iRecordCount"] == null) ? this._iRecordCount : (int)ViewState["iRecordCount"]);
}
}
private int _iRowsCount = 10;
/**//// <summary>
/// 每页记录数量
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("每页记录数量")]
[DefaultValueAttribute("每页记录数量")]
public int iRowsCount
{
get
{
return ((ViewState["iRowsCount"] == null) ? this._iRowsCount : (int)ViewState["iRowsCount"]);
}
set
{
this._iRowsCount = value;
ViewState["iRowsCount"] = value;
}
}
private int _iPrevCount = 5;
/**//// <summary>
/// 前部分记录数量
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("前部分记录数量")]
[DefaultValueAttribute("前部分记录数量")]
public int iPrevCount
{
get
{
return ((ViewState["iPrevCount"] == null) ? this._iPrevCount : (int)ViewState["iPrevCount"]);
}
set
{
this._iPrevCount = value;
ViewState["iPrevCount"] = value;
}
}
private int _iNextCount = 5;
/**//// <summary>
/// 后部分记录数量
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("后部分记录数量")]
[DefaultValueAttribute("后部分记录数量")]
public int iNextCount
{
get
{
return ((ViewState["iNextCount"] == null) ? this._iNextCount : (int)ViewState["iNextCount"]);
}
set
{
ViewState["iNextCount"] = value;
}
}
private static readonly object EventSubmitKey = new object();
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("首页导航按钮文本")]
[DefaultValueAttribute("首页")]
public string goFirstText
{
get
{
return ((ViewState["goFirstText"] == null) ? "首页" : (string)ViewState["goFirstText"]);
}
set
{
ViewState["goFirstText"] = value;
}
}
//页面总数
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int pageCount
{
get
{
if (this.iRecordCount == 0)
return 1;
return (int)Math.Ceiling((double)this.iRecordCount / (double)this.iRowsCount);
}
}
//如果小于此数字则不显示省略号
private int intDefaultCount = 10;
/**//// <summary>
/// 上一页按钮文本
/// </summary>
[Browsable(true), Themeable(true), DefaultValue("<<上一页"), DescriptionAttribute("上一页按钮文本")]
public string PrevPageText
{
get
{
return (ViewState["PrevPageText"] == null) ? "<<上一页" : (string)ViewState["PrevPageText"];
}
set { ViewState["PrevPageText"] = value; }
}
/**//// <summary>
/// 下一页按钮文本
/// </summary>
[Browsable(true), Themeable(true), DefaultValue("下一页>>"), DescriptionAttribute("下一页按钮文本")]
public string NextPageText
{
get
{
return (ViewState["NextPageText"] == null) ? "下一页>>" : (string)ViewState["NextPageText"];
}
set { ViewState["NextPageText"] = value; }
}
/**//// <summary>
/// 末页按钮文本
/// </summary>
[Browsable(true), Themeable(true), DefaultValue("末页"), DescriptionAttribute("末页按钮文本")]
public string LastPageText
{
get
{
return (ViewState["LastPageText"] == null) ? "末页" : (string)ViewState["LastPageText"];
}
set { ViewState["LastPageText"] = value; }
}
/**//// <summary>
/// 是否显示文本框
/// </summary>
[Browsable(true), Themeable(true), DefaultValue(false), DescriptionAttribute("是否显示文本框")]
public bool IsInput
{
get
{
return (ViewState["IsInput"] == null) ? false : (bool)ViewState["IsInput"];
}
set { ViewState["IsInput"] = value; }
}
/**//// <summary>
/// 是否url分页
/// </summary>
[Browsable(true), Themeable(true), DefaultValue(false ), DescriptionAttribute("是否url分页")]
public bool UrlPaging
{
get
{
return (ViewState["UrlPaging"] == null) ? false : (bool)ViewState["UrlPaging"];
}
set { ViewState["UrlPaging"] = value; }
}
[Browsable(true), DefaultValue("page"), DescriptionAttribute("分页参数")]
public string UrlPageIndexName
{
get
{
return (null == ViewState["UrlPageIndexName"]) ? "page" : (string)ViewState["UrlPageIndexName"];
}
set { ViewState["UrlPageIndexName"] = value; }
}
/**//// <summary>
/// 是否显示首页
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("是否显示首页")]
[DefaultValue(true )]
public bool IsDisplayFirstText
{
get
{
return ((ViewState["IsDisplayFirst"] == null) ? true : (bool)ViewState["IsDisplayFirst"]);
}
set
{
ViewState["IsDisplayFirst"] = value;
}
}
/**//// <summary>
/// 是否显示末页
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("是否显示末页")]
[DefaultValue(true )]
public bool IsDisplayLastText
{
get
{
return ((ViewState["IsDisplayLastText"] == null) ? true : (bool)ViewState["IsDisplayLastText"]);
}
set
{
ViewState["IsDisplayLastText"] = value;
}
}
#endregion
Methods#region Methods
/**//// <summary>
/// 取得默认样式表信息
/// by minjiang 08-3-24
/// </summary>
/// <returns></returns>
private void AddStyleFile(HtmlTextWriter output)
{
//如果是设计模式则返回 只有在有页面实体真正存在的情况才加载样式
if (DesignMode)
{ return; }
//分页样式表信息
样式内容#region 样式内容
//Get the name of the Web Resource.
string rsname = "pagerControls.Resources.listPager.css";
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
string sSre = cs.GetWebResourceUrl(this .GetType (), rsname);
output.AddAttribute(HtmlTextWriterAttribute.Href, sSre );
output.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
output.AddAttribute(HtmlTextWriterAttribute.Type , "text/css");
output.RenderBeginTag(HtmlTextWriterTag.Link);
output.RenderEndTag();
#endregion
}
/**//// <summary>
/// 链接索引数 从1开始
/// 写链接样式
/// </summary>
/// <param name="kk">链接索引数 从1开始</param>
/// <param name="output"></param>
private void writeUrlCss(int kk, HtmlTextWriter output)
{
//链接样式
string sCss = "";
if (this.currentPageIndex == kk)
{
sCss = "select a5";
}
if (sCss != "")
{
output.AddAttribute(HtmlTextWriterAttribute.Class, sCss);
}
}
/**//// <summary>
/// 取得分页信息
/// by minjiang 08-3-11
/// </summary>
/// <param name="recordCount">记录总数</param>
/// <param name="iRowsCount">每页记录大小</param>
/// <param name="pageUrl">页面地址</param>
/// <returns></returns>
public string CSgetPagerHtml(int recordCount, int iRowsCount, HtmlTextWriter output)
{
if (pageCount <= 1)
{
return "";
}
output.AddAttribute(HtmlTextWriterAttribute.Class , "page", false);
output.RenderBeginTag(HtmlTextWriterTag.P );
if (this.currentPageIndex < 1)
{
this.currentPageIndex = 1;
}
if (this.currentPageIndex > pageCount)
{ this.currentPageIndex = 1; }
//上一页索引
int prevPage = 1;
prevPage = this.currentPageIndex - 1;
if (prevPage < 1)
{ prevPage = 1; }
//下一页索引
int nextPage = 1;
nextPage = this.currentPageIndex + 1;
if (nextPage > pageCount)
{ nextPage = 1; }
//开始的索引页
int startPageIndex = this.currentPageIndex;
//要统计的页面总数
int pageTotalCount = pageCount - this.currentPageIndex + 1;
//如果当前页面不是首页则显示上一页
if (this.currentPageIndex != 1)
{
this.writeLinkHref(output, prevPage, true,this .PrevPageText );
}
else
{
this.writeLinkHref(output, 1 , false,this .PrevPageText );
}
if (IsDisplayFirstText)
{
//回首页
if (this.currentPageIndex != 1)
{
//如果当前页面不是首页则给首页文本加上链接
this.writeLinkHref(output, 1, true,this .goFirstText );
}
else
{
this.writeLinkHref(output, 1 , false,this .goFirstText );
}
}
//如果页数小于等于10页则不显示省略号
intDefaultCount = this.iNextCount + this.iPrevCount;
数字按钮内容#region 数字按钮内容
output.RenderBeginTag(HtmlTextWriterTag.Span );
if (pageCount <= intDefaultCount)
{
for (int kk = 1; kk <= pageCount; kk++)
{
this.writeLinkHref(output, kk, true,"");
}
}
else
{
记录数多于10#region 记录数多于10
//省略号前显示5页
if (pageTotalCount <= intDefaultCount)
{
//如果要统计的页面数量小于分页前部分数量加分页后部分数量
//要统计的页面数量与分页前部分数量加分页后部分数量的差额
//分页栏总是显示分页前部分数量加分页后部分数量个页面链接
for (int k = 1; k <= this.intDefaultCount; k++)
{
//当前页面数字
int iIndex = pageCount - this.intDefaultCount + k;
this.writeLinkHref(output,iIndex, true,"");
}
}
else
{
for (int k = 1; k <= this.iPrevCount; k++)
{
//当前页面数字
int iIndex = this.currentPageIndex + k - 1;
this.writeLinkHref(output, iIndex, true,"");
}
if (pageTotalCount > intDefaultCount)
{
//如果要统计的页面总数大于省略号前的记录加省略号后面的记录数量则显示 ""
output.Write(" ");
}
for (int j = this.iPrevCount + 1; j <= this.intDefaultCount; j++)
{
//当前页面数字
int iIndex = pageCount - this.iNextCount + j - this.iPrevCount ;
this.writeLinkHref(output, iIndex, true,"");
}
}
#endregion
}
output.RenderEndTag();
#endregion
if (IsDisplayLastText)
{
//如果页面总数大于1则显示末页按钮
if (this.pageCount > 1)
{
if (this.currentPageIndex != pageCount)
{
this.writeLinkHref(output, pageCount, true,this .LastPageText );
}
else
{
this.writeLinkHref(output, 1 , false ,this .LastPageText );
}
}
}
//下一页
if (this.currentPageIndex != pageCount)
{
this.writeLinkHref(output, nextPage, true,this .NextPageText );
}
else
{
this.writeLinkHref(output, 1 , false,this .NextPageText );
}
if (this.IsInput)
{
//如果显示跳转文本框
this.writeInput(output );
}
//标记结尾标记
output.RenderEndTag();
return "";
}
/**//// <summary>
/// 输出linkBtn链接地址属性
/// </summary>
/// <param name="output"></param>
/// <param name="iValue"></param>
/// <param name="IsDiplayUrl">是否显示文本外国投资</param>
private void writeLinkHref(HtmlTextWriter output,int iValue,bool IsDiplayUrl,string navText)
{
if (IsDiplayUrl)
{
if (this.UrlPaging == true && !DesignMode)
{
//url分页
//当前页面地址
string pageUrl = Page.Request.Url.ToString();
//链接地址
if (pageUrl.IndexOf(this.UrlPageIndexName + "=") != -1)
{
//已经存在分页参数
//参数在链接中的索引
int pageIndex = pageUrl.IndexOf(this.UrlPageIndexName + "=");
//判断参数后面是否还有其它的参数
//余下的字符串
string sLast = pageUrl.Substring(pageIndex + this.UrlPageIndexName.Length);
string s = "";
if (sLast.IndexOf("&") != -1)
{
if (sLast.IndexOf("=") != -1)
{
//说明有参数
//第一个参数的索引
int iFirstIndex = sLast.IndexOf("&");
s = sLast.Substring(iFirstIndex);
}
}
pageUrl = pageUrl.Substring(0, pageIndex + this.UrlPageIndexName.Length + 1) + iValue.ToString() + s;
}
else
{
//如果页面没有任何参数则加上?与参数
if (pageUrl.IndexOf("?") == -1)
{
pageUrl += "?" + this.UrlPageIndexName + "=";
}
else
{
//如果只是没有page参数则加上些参数
pageUrl += "&" + this.UrlPageIndexName + "=";
}
pageUrl += iValue.ToString();
}
this.writeUrlCss(iValue , output);
output.AddAttribute(HtmlTextWriterAttribute.Href, pageUrl);
output.RenderBeginTag(HtmlTextWriterTag.A);
if (navText != "")
{
output.Write(navText );
}
else
{
output.Write(iValue.ToString ());
}
output.RenderEndTag();
output.Write(" ");
}
else
{
this.writeUrlCss(iValue, output);
output.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(iValue));
output.RenderBeginTag(HtmlTextWriterTag.A);
if (navText != "")
{
output.Write(navText);
}
else
{
output.Write(iValue.ToString ());
}
output.RenderEndTag();
output.Write(" ");
}
}
else
{
output.Write(navText );
output.Write(" ");
}
}
//跳转到哪一页
private string inputPageIndex;
/**//// <summary>
/// 跳转文本框
/// </summary>
/// <param name="writer"></param>
private void writeInput(HtmlTextWriter writer)
{
//输入文本框
string boxClientId = UniqueID + "_input";
writer.Write(" ");
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "30px");
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.currentPageIndex .ToString());
if (pageCount <= 1)
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
writer.AddAttribute(HtmlTextWriterAttribute.Name, boxClientId);
writer.AddAttribute(HtmlTextWriterAttribute.Id, boxClientId);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.Write(" ");
//提交按钮
int iInputIndex = 1;
int.TryParse(inputPageIndex,out iInputIndex );
if (iInputIndex < 1)
{ iInputIndex = 1; }
if (iInputIndex > pageCount)
{ iInputIndex = 1; }
writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString());
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.Write("转到");
writer.RenderEndTag();
}
#endregion
控件本身方法#region 控件本身方法
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
protected override void RenderContents(HtmlTextWriter output)
{
AddAttributesToRender(output);
//分页样式表信息
if (!this.IsCustomStyle)
{
this.AddStyleFile(output);
}
//写入分页信息
this.CSgetPagerHtml(this.iRecordCount, this.iRowsCount, output);
}
/**//// <summary>
/// 控件加载事件
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
int i=0;
if (this.UrlPaging==true &&!DesignMode )
{
//当前页面地址
string currentUrl = Page.Request.Url .ToString ();
if (!Page.IsPostBack)
{
int index;
int.TryParse(Page.Request.QueryString[UrlPageIndexName], out index);
if (index <= 0)
index = 1;
if(index >this .pageCount )
{index =1;}
PageChangingEventArgs args = new PageChangingEventArgs(index);
OnPageChanging(args);
}
}
if (!DesignMode)
{
inputPageIndex = Page.Request.Form[UniqueID + "_input"];
int .TryParse (inputPageIndex ,out i);
if (i < 1 || i > this.pageCount)
{ i = 1; }
inputPageIndex = i.ToString();
}
base.OnLoad(e);
}
#endregion
IPostBackEventHandler Implementation#region IPostBackEventHandler Implementation
/**//// <summary>
/// 控件回发事件处理方法
/// 实现 IPostBackEventHandle接口
/// </summary>
/// <param name="args">参数名称</param>
public void RaisePostBackEvent(string args)
{
int pageIndex = this .currentPageIndex ;
try
{
if (args == null || args == "")
args = inputPageIndex;
pageIndex = int.Parse(args);
}
catch { }
PageChangingEventArgs pcArgs = new PageChangingEventArgs(pageIndex);
OnPageChanging(pcArgs);
}
#endregion
IPostBackDataHandler Implementation#region IPostBackDataHandler Implementation
/**//// <summary>
/// 定义 ASP.NET 服务器控件为自动加载回发数据而必须实现的方法
/// </summary>
/// <param name="pkey"></param>
/// <param name="pcol"></param>
/// <returns></returns>
public virtual bool LoadPostData(string pkey, NameValueCollection pcol)
{
string str = pcol[UniqueID + "_input"];
if (str != null && str.Trim().Length > 0)
{
try
{
int pindex = int.Parse(str);
if (pindex > 0 && pindex <= this.pageCount)
{
inputPageIndex = str;
Page.RegisterRequiresRaiseEvent(this);
}
}
catch { }
}
return false;
}
/**//// ASP.NET 服务器控件为自动加载回发数据而必须实现的方法
public virtual void RaisePostDataChangedEvent() { }
#endregion
/**//// <summary>
/// 注册有参数的linkbtn事件
/// </summary>
/// <param name="pageIndex">参数值.</param>
/// <returns>.</returns>
private string GetHrefString(int pageIndex)
{
if (!DesignMode)
{
return Page.ClientScript.GetPostBackClientHyperlink(this, pageIndex.ToString());
}
else { return ""; }
}
/**//// <summary>
/// 注册没有参数的linkbtn事件
/// </summary>
/// <returns></returns>
private string GetHrefString()
{
if (!DesignMode)
{
return Page.ClientScript.GetPostBackClientHyperlink(this, "");
}
else { return ""; }
}
Private fields#region Private fields
private static readonly object EventPageChanging = new object();
private static readonly object EventPageChanged = new object();
#endregion
Events#region Events
/**//// <summary>
/// 分页前事件
/// </summary>
public event PageChangingEventHandler PageChanging
{
add
{
Events.AddHandler(EventPageChanging, value);
}
remove
{
Events.RemoveHandler(EventPageChanging, value);
}
}
/**//// <summary>
/// 分页完成后事件
/// </summary>
public event EventHandler PageChanged
{
add
{
Events.AddHandler(EventPageChanged, value);
}
remove
{
Events.RemoveHandler(EventPageChanged, value);
}
}
#endregion
EventsMethods#region EventsMethods
/**//// <summary>
/// 分页前事件处理方法
/// </summary>
/// <param name="e"></param>
protected virtual void OnPageChanging(PageChangingEventArgs e)
{
PageChangingEventHandler handler = (PageChangingEventHandler)Events[EventPageChanging];
if (handler != null)
{
handler(this, e);
}
this.currentPageIndex = e.NewPageIndex;
OnPageChanged(EventArgs.Empty);
}
/**//// <summary>
/// 分页完成后事件处理方法
/// </summary>
/// <param name="e"></param>
protected virtual void OnPageChanged(EventArgs e)
{
EventHandler handler = (EventHandler)Events[EventPageChanged];
if (handler != null)
handler(this, e);
}
#endregion
}
PageChangingEventHandler Delegate#region PageChangingEventHandler Delegate
/**//// <summary>
/// 分页前委托
/// </summary>
/// <param name="src"></param>
/// <param name="e"></param>
public delegate void PageChangingEventHandler(object src, PageChangingEventArgs e);
#endregion
PageChangingEventArgs Class#region PageChangingEventArgs Class
/**//// <summary>
/// 分页委托参数类
/// </summary>
public sealed class PageChangingEventArgs : CancelEventArgs
{
//当前页面
private readonly int _newpageindex;
public PageChangingEventArgs(int newPageIndex)
{
_newpageindex = newPageIndex;
}
public int NewPageIndex
{
get { return _newpageindex; }
}
}
#endregion
}
using System;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
[assembly: WebResource("pagerControls.Resources.listPager.css", "text/css", PerformSubstitution = true)]
namespace pagerControls
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:WebCustomControlPager runat=server></{0}:WebCustomControlPager>")]
[ParseChildren(true)]
public class WebCustomControlPager : CompositeControl, INamingContainer, IPostBackEventHandler, IPostBackDataHandler
{
Properties#region Properties
private int _currentPageIndex = 1;
/**//// <summary>
/// 当前页面索引
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("当前页面索引从1开始")]
[DefaultValueAttribute("当前页面索引")]
public int currentPageIndex
{
get
{
return ((ViewState["currentPageIndex"] == null) ? this._currentPageIndex : (int)ViewState["currentPageIndex"]);
}
set
{
ViewState["currentPageIndex"] = value;
}
}
/**//// <summary>
/// 是否自定义样式
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("是否自定义样式")]
[DefaultValueAttribute("0")]
public bool IsCustomStyle
{
get
{
return ((ViewState["IsCustomStyle"] == null) ? false : (bool)ViewState["IsCustomStyle"]);
}
set
{
ViewState["IsCustomStyle"] = value;
}
}
private int _iRecordCount = 1;
/**//// <summary>
/// 记录数量
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("记录数量")]
[DefaultValueAttribute("记录数量")]
public int iRecordCount
{
set
{
ViewState["iRecordCount"] = value;
this._iRecordCount = value;
}
get
{
return ((ViewState["iRecordCount"] == null) ? this._iRecordCount : (int)ViewState["iRecordCount"]);
}
}
private int _iRowsCount = 10;
/**//// <summary>
/// 每页记录数量
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("每页记录数量")]
[DefaultValueAttribute("每页记录数量")]
public int iRowsCount
{
get
{
return ((ViewState["iRowsCount"] == null) ? this._iRowsCount : (int)ViewState["iRowsCount"]);
}
set
{
this._iRowsCount = value;
ViewState["iRowsCount"] = value;
}
}
private int _iPrevCount = 5;
/**//// <summary>
/// 前部分记录数量
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("前部分记录数量")]
[DefaultValueAttribute("前部分记录数量")]
public int iPrevCount
{
get
{
return ((ViewState["iPrevCount"] == null) ? this._iPrevCount : (int)ViewState["iPrevCount"]);
}
set
{
this._iPrevCount = value;
ViewState["iPrevCount"] = value;
}
}
private int _iNextCount = 5;
/**//// <summary>
/// 后部分记录数量
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("后部分记录数量")]
[DefaultValueAttribute("后部分记录数量")]
public int iNextCount
{
get
{
return ((ViewState["iNextCount"] == null) ? this._iNextCount : (int)ViewState["iNextCount"]);
}
set
{
ViewState["iNextCount"] = value;
}
}
private static readonly object EventSubmitKey = new object();
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("首页导航按钮文本")]
[DefaultValueAttribute("首页")]
public string goFirstText
{
get
{
return ((ViewState["goFirstText"] == null) ? "首页" : (string)ViewState["goFirstText"]);
}
set
{
ViewState["goFirstText"] = value;
}
}
//页面总数
[Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public int pageCount
{
get
{
if (this.iRecordCount == 0)
return 1;
return (int)Math.Ceiling((double)this.iRecordCount / (double)this.iRowsCount);
}
}
//如果小于此数字则不显示省略号
private int intDefaultCount = 10;
/**//// <summary>
/// 上一页按钮文本
/// </summary>
[Browsable(true), Themeable(true), DefaultValue("<<上一页"), DescriptionAttribute("上一页按钮文本")]
public string PrevPageText
{
get
{
return (ViewState["PrevPageText"] == null) ? "<<上一页" : (string)ViewState["PrevPageText"];
}
set { ViewState["PrevPageText"] = value; }
}
/**//// <summary>
/// 下一页按钮文本
/// </summary>
[Browsable(true), Themeable(true), DefaultValue("下一页>>"), DescriptionAttribute("下一页按钮文本")]
public string NextPageText
{
get
{
return (ViewState["NextPageText"] == null) ? "下一页>>" : (string)ViewState["NextPageText"];
}
set { ViewState["NextPageText"] = value; }
}
/**//// <summary>
/// 末页按钮文本
/// </summary>
[Browsable(true), Themeable(true), DefaultValue("末页"), DescriptionAttribute("末页按钮文本")]
public string LastPageText
{
get
{
return (ViewState["LastPageText"] == null) ? "末页" : (string)ViewState["LastPageText"];
}
set { ViewState["LastPageText"] = value; }
}
/**//// <summary>
/// 是否显示文本框
/// </summary>
[Browsable(true), Themeable(true), DefaultValue(false), DescriptionAttribute("是否显示文本框")]
public bool IsInput
{
get
{
return (ViewState["IsInput"] == null) ? false : (bool)ViewState["IsInput"];
}
set { ViewState["IsInput"] = value; }
}
/**//// <summary>
/// 是否url分页
/// </summary>
[Browsable(true), Themeable(true), DefaultValue(false ), DescriptionAttribute("是否url分页")]
public bool UrlPaging
{
get
{
return (ViewState["UrlPaging"] == null) ? false : (bool)ViewState["UrlPaging"];
}
set { ViewState["UrlPaging"] = value; }
}
[Browsable(true), DefaultValue("page"), DescriptionAttribute("分页参数")]
public string UrlPageIndexName
{
get
{
return (null == ViewState["UrlPageIndexName"]) ? "page" : (string)ViewState["UrlPageIndexName"];
}
set { ViewState["UrlPageIndexName"] = value; }
}
/**//// <summary>
/// 是否显示首页
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("是否显示首页")]
[DefaultValue(true )]
public bool IsDisplayFirstText
{
get
{
return ((ViewState["IsDisplayFirst"] == null) ? true : (bool)ViewState["IsDisplayFirst"]);
}
set
{
ViewState["IsDisplayFirst"] = value;
}
}
/**//// <summary>
/// 是否显示末页
/// </summary>
[Bindable(true)]
[Localizable(true)]
[DescriptionAttribute("是否显示末页")]
[DefaultValue(true )]
public bool IsDisplayLastText
{
get
{
return ((ViewState["IsDisplayLastText"] == null) ? true : (bool)ViewState["IsDisplayLastText"]);
}
set
{
ViewState["IsDisplayLastText"] = value;
}
}
#endregion
Methods#region Methods
/**//// <summary>
/// 取得默认样式表信息
/// by minjiang 08-3-24
/// </summary>
/// <returns></returns>
private void AddStyleFile(HtmlTextWriter output)
{
//如果是设计模式则返回 只有在有页面实体真正存在的情况才加载样式
if (DesignMode)
{ return; }
//分页样式表信息
样式内容#region 样式内容
//Get the name of the Web Resource.
string rsname = "pagerControls.Resources.listPager.css";
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
string sSre = cs.GetWebResourceUrl(this .GetType (), rsname);
output.AddAttribute(HtmlTextWriterAttribute.Href, sSre );
output.AddAttribute(HtmlTextWriterAttribute.Rel, "stylesheet");
output.AddAttribute(HtmlTextWriterAttribute.Type , "text/css");
output.RenderBeginTag(HtmlTextWriterTag.Link);
output.RenderEndTag();
#endregion
}
/**//// <summary>
/// 链接索引数 从1开始
/// 写链接样式
/// </summary>
/// <param name="kk">链接索引数 从1开始</param>
/// <param name="output"></param>
private void writeUrlCss(int kk, HtmlTextWriter output)
{
//链接样式
string sCss = "";
if (this.currentPageIndex == kk)
{
sCss = "select a5";
}
if (sCss != "")
{
output.AddAttribute(HtmlTextWriterAttribute.Class, sCss);
}
}
/**//// <summary>
/// 取得分页信息
/// by minjiang 08-3-11
/// </summary>
/// <param name="recordCount">记录总数</param>
/// <param name="iRowsCount">每页记录大小</param>
/// <param name="pageUrl">页面地址</param>
/// <returns></returns>
public string CSgetPagerHtml(int recordCount, int iRowsCount, HtmlTextWriter output)
{
if (pageCount <= 1)
{
return "";
}
output.AddAttribute(HtmlTextWriterAttribute.Class , "page", false);
output.RenderBeginTag(HtmlTextWriterTag.P );
if (this.currentPageIndex < 1)
{
this.currentPageIndex = 1;
}
if (this.currentPageIndex > pageCount)
{ this.currentPageIndex = 1; }
//上一页索引
int prevPage = 1;
prevPage = this.currentPageIndex - 1;
if (prevPage < 1)
{ prevPage = 1; }
//下一页索引
int nextPage = 1;
nextPage = this.currentPageIndex + 1;
if (nextPage > pageCount)
{ nextPage = 1; }
//开始的索引页
int startPageIndex = this.currentPageIndex;
//要统计的页面总数
int pageTotalCount = pageCount - this.currentPageIndex + 1;
//如果当前页面不是首页则显示上一页
if (this.currentPageIndex != 1)
{
this.writeLinkHref(output, prevPage, true,this .PrevPageText );
}
else
{
this.writeLinkHref(output, 1 , false,this .PrevPageText );
}
if (IsDisplayFirstText)
{
//回首页
if (this.currentPageIndex != 1)
{
//如果当前页面不是首页则给首页文本加上链接
this.writeLinkHref(output, 1, true,this .goFirstText );
}
else
{
this.writeLinkHref(output, 1 , false,this .goFirstText );
}
}
//如果页数小于等于10页则不显示省略号
intDefaultCount = this.iNextCount + this.iPrevCount;
数字按钮内容#region 数字按钮内容
output.RenderBeginTag(HtmlTextWriterTag.Span );
if (pageCount <= intDefaultCount)
{
for (int kk = 1; kk <= pageCount; kk++)
{
this.writeLinkHref(output, kk, true,"");
}
}
else
{
记录数多于10#region 记录数多于10
//省略号前显示5页
if (pageTotalCount <= intDefaultCount)
{
//如果要统计的页面数量小于分页前部分数量加分页后部分数量
//要统计的页面数量与分页前部分数量加分页后部分数量的差额
//分页栏总是显示分页前部分数量加分页后部分数量个页面链接
for (int k = 1; k <= this.intDefaultCount; k++)
{
//当前页面数字
int iIndex = pageCount - this.intDefaultCount + k;
this.writeLinkHref(output,iIndex, true,"");
}
}
else
{
for (int k = 1; k <= this.iPrevCount; k++)
{
//当前页面数字
int iIndex = this.currentPageIndex + k - 1;
this.writeLinkHref(output, iIndex, true,"");
}
if (pageTotalCount > intDefaultCount)
{
//如果要统计的页面总数大于省略号前的记录加省略号后面的记录数量则显示 ""
output.Write(" ");
}
for (int j = this.iPrevCount + 1; j <= this.intDefaultCount; j++)
{
//当前页面数字
int iIndex = pageCount - this.iNextCount + j - this.iPrevCount ;
this.writeLinkHref(output, iIndex, true,"");
}
}
#endregion
}
output.RenderEndTag();
#endregion
if (IsDisplayLastText)
{
//如果页面总数大于1则显示末页按钮
if (this.pageCount > 1)
{
if (this.currentPageIndex != pageCount)
{
this.writeLinkHref(output, pageCount, true,this .LastPageText );
}
else
{
this.writeLinkHref(output, 1 , false ,this .LastPageText );
}
}
}
//下一页
if (this.currentPageIndex != pageCount)
{
this.writeLinkHref(output, nextPage, true,this .NextPageText );
}
else
{
this.writeLinkHref(output, 1 , false,this .NextPageText );
}
if (this.IsInput)
{
//如果显示跳转文本框
this.writeInput(output );
}
//标记结尾标记
output.RenderEndTag();
return "";
}
/**//// <summary>
/// 输出linkBtn链接地址属性
/// </summary>
/// <param name="output"></param>
/// <param name="iValue"></param>
/// <param name="IsDiplayUrl">是否显示文本外国投资</param>
private void writeLinkHref(HtmlTextWriter output,int iValue,bool IsDiplayUrl,string navText)
{
if (IsDiplayUrl)
{
if (this.UrlPaging == true && !DesignMode)
{
//url分页
//当前页面地址
string pageUrl = Page.Request.Url.ToString();
//链接地址
if (pageUrl.IndexOf(this.UrlPageIndexName + "=") != -1)
{
//已经存在分页参数
//参数在链接中的索引
int pageIndex = pageUrl.IndexOf(this.UrlPageIndexName + "=");
//判断参数后面是否还有其它的参数
//余下的字符串
string sLast = pageUrl.Substring(pageIndex + this.UrlPageIndexName.Length);
string s = "";
if (sLast.IndexOf("&") != -1)
{
if (sLast.IndexOf("=") != -1)
{
//说明有参数
//第一个参数的索引
int iFirstIndex = sLast.IndexOf("&");
s = sLast.Substring(iFirstIndex);
}
}
pageUrl = pageUrl.Substring(0, pageIndex + this.UrlPageIndexName.Length + 1) + iValue.ToString() + s;
}
else
{
//如果页面没有任何参数则加上?与参数
if (pageUrl.IndexOf("?") == -1)
{
pageUrl += "?" + this.UrlPageIndexName + "=";
}
else
{
//如果只是没有page参数则加上些参数
pageUrl += "&" + this.UrlPageIndexName + "=";
}
pageUrl += iValue.ToString();
}
this.writeUrlCss(iValue , output);
output.AddAttribute(HtmlTextWriterAttribute.Href, pageUrl);
output.RenderBeginTag(HtmlTextWriterTag.A);
if (navText != "")
{
output.Write(navText );
}
else
{
output.Write(iValue.ToString ());
}
output.RenderEndTag();
output.Write(" ");
}
else
{
this.writeUrlCss(iValue, output);
output.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString(iValue));
output.RenderBeginTag(HtmlTextWriterTag.A);
if (navText != "")
{
output.Write(navText);
}
else
{
output.Write(iValue.ToString ());
}
output.RenderEndTag();
output.Write(" ");
}
}
else
{
output.Write(navText );
output.Write(" ");
}
}
//跳转到哪一页
private string inputPageIndex;
/**//// <summary>
/// 跳转文本框
/// </summary>
/// <param name="writer"></param>
private void writeInput(HtmlTextWriter writer)
{
//输入文本框
string boxClientId = UniqueID + "_input";
writer.Write(" ");
writer.AddAttribute(HtmlTextWriterAttribute.Type, "text");
writer.AddStyleAttribute(HtmlTextWriterStyle.Width, "30px");
writer.AddAttribute(HtmlTextWriterAttribute.Value, this.currentPageIndex .ToString());
if (pageCount <= 1)
writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
writer.AddAttribute(HtmlTextWriterAttribute.Name, boxClientId);
writer.AddAttribute(HtmlTextWriterAttribute.Id, boxClientId);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
writer.Write(" ");
//提交按钮
int iInputIndex = 1;
int.TryParse(inputPageIndex,out iInputIndex );
if (iInputIndex < 1)
{ iInputIndex = 1; }
if (iInputIndex > pageCount)
{ iInputIndex = 1; }
writer.AddAttribute(HtmlTextWriterAttribute.Href, GetHrefString());
writer.RenderBeginTag(HtmlTextWriterTag.A);
writer.Write("转到");
writer.RenderEndTag();
}
#endregion
控件本身方法#region 控件本身方法
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
}
protected override void RenderContents(HtmlTextWriter output)
{
AddAttributesToRender(output);
//分页样式表信息
if (!this.IsCustomStyle)
{
this.AddStyleFile(output);
}
//写入分页信息
this.CSgetPagerHtml(this.iRecordCount, this.iRowsCount, output);
}
/**//// <summary>
/// 控件加载事件
/// </summary>
/// <param name="e"></param>
protected override void OnLoad(EventArgs e)
{
int i=0;
if (this.UrlPaging==true &&!DesignMode )
{
//当前页面地址
string currentUrl = Page.Request.Url .ToString ();
if (!Page.IsPostBack)
{
int index;
int.TryParse(Page.Request.QueryString[UrlPageIndexName], out index);
if (index <= 0)
index = 1;
if(index >this .pageCount )
{index =1;}
PageChangingEventArgs args = new PageChangingEventArgs(index);
OnPageChanging(args);
}
}
if (!DesignMode)
{
inputPageIndex = Page.Request.Form[UniqueID + "_input"];
int .TryParse (inputPageIndex ,out i);
if (i < 1 || i > this.pageCount)
{ i = 1; }
inputPageIndex = i.ToString();
}
base.OnLoad(e);
}
#endregion
IPostBackEventHandler Implementation#region IPostBackEventHandler Implementation
/**//// <summary>
/// 控件回发事件处理方法
/// 实现 IPostBackEventHandle接口
/// </summary>
/// <param name="args">参数名称</param>
public void RaisePostBackEvent(string args)
{
int pageIndex = this .currentPageIndex ;
try
{
if (args == null || args == "")
args = inputPageIndex;
pageIndex = int.Parse(args);
}
catch { }
PageChangingEventArgs pcArgs = new PageChangingEventArgs(pageIndex);
OnPageChanging(pcArgs);
}
#endregion
IPostBackDataHandler Implementation#region IPostBackDataHandler Implementation
/**//// <summary>
/// 定义 ASP.NET 服务器控件为自动加载回发数据而必须实现的方法
/// </summary>
/// <param name="pkey"></param>
/// <param name="pcol"></param>
/// <returns></returns>
public virtual bool LoadPostData(string pkey, NameValueCollection pcol)
{
string str = pcol[UniqueID + "_input"];
if (str != null && str.Trim().Length > 0)
{
try
{
int pindex = int.Parse(str);
if (pindex > 0 && pindex <= this.pageCount)
{
inputPageIndex = str;
Page.RegisterRequiresRaiseEvent(this);
}
}
catch { }
}
return false;
}
/**//// ASP.NET 服务器控件为自动加载回发数据而必须实现的方法
public virtual void RaisePostDataChangedEvent() { }
#endregion
/**//// <summary>
/// 注册有参数的linkbtn事件
/// </summary>
/// <param name="pageIndex">参数值.</param>
/// <returns>.</returns>
private string GetHrefString(int pageIndex)
{
if (!DesignMode)
{
return Page.ClientScript.GetPostBackClientHyperlink(this, pageIndex.ToString());
}
else { return ""; }
}
/**//// <summary>
/// 注册没有参数的linkbtn事件
/// </summary>
/// <returns></returns>
private string GetHrefString()
{
if (!DesignMode)
{
return Page.ClientScript.GetPostBackClientHyperlink(this, "");
}
else { return ""; }
}
Private fields#region Private fields
private static readonly object EventPageChanging = new object();
private static readonly object EventPageChanged = new object();
#endregion
Events#region Events
/**//// <summary>
/// 分页前事件
/// </summary>
public event PageChangingEventHandler PageChanging
{
add
{
Events.AddHandler(EventPageChanging, value);
}
remove
{
Events.RemoveHandler(EventPageChanging, value);
}
}
/**//// <summary>
/// 分页完成后事件
/// </summary>
public event EventHandler PageChanged
{
add
{
Events.AddHandler(EventPageChanged, value);
}
remove
{
Events.RemoveHandler(EventPageChanged, value);
}
}
#endregion
EventsMethods#region EventsMethods
/**//// <summary>
/// 分页前事件处理方法
/// </summary>
/// <param name="e"></param>
protected virtual void OnPageChanging(PageChangingEventArgs e)
{
PageChangingEventHandler handler = (PageChangingEventHandler)Events[EventPageChanging];
if (handler != null)
{
handler(this, e);
}
this.currentPageIndex = e.NewPageIndex;
OnPageChanged(EventArgs.Empty);
}
/**//// <summary>
/// 分页完成后事件处理方法
/// </summary>
/// <param name="e"></param>
protected virtual void OnPageChanged(EventArgs e)
{
EventHandler handler = (EventHandler)Events[EventPageChanged];
if (handler != null)
handler(this, e);
}
#endregion
}
PageChangingEventHandler Delegate#region PageChangingEventHandler Delegate
/**//// <summary>
/// 分页前委托
/// </summary>
/// <param name="src"></param>
/// <param name="e"></param>
public delegate void PageChangingEventHandler(object src, PageChangingEventArgs e);
#endregion
PageChangingEventArgs Class#region PageChangingEventArgs Class
/**//// <summary>
/// 分页委托参数类
/// </summary>
public sealed class PageChangingEventArgs : CancelEventArgs
{
//当前页面
private readonly int _newpageindex;
public PageChangingEventArgs(int newPageIndex)
{
_newpageindex = newPageIndex;
}
public int NewPageIndex
{
get { return _newpageindex; }
}
}
#endregion
}