using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Frame.Component
{
[Serializable]
public class PageArgs : EventArgs
{
public PageArgs()
{
}
private int _curPage;
public int CurPage
{
get
{
return _curPage;
}
set
{
_curPage = value;
}
}
}
public delegate void EventPageIndex(Object sender, PageArgs e);
[DefaultProperty("RecordCount")]
[ToolboxData("<{0}:Pager runat=server></{0}:Pager>")]
public class Pager : WebControl, IPostBackEventHandler
{
#region 事件
public event EventPageIndex PageChange; //页更改事件
public virtual void OnPageChange(Object sender, PageArgs e)
{
if (PageChange != null)
{
PageChange(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
this.CurPage = Int32.Parse(System.Web.HttpContext.Current.Request.Form[this.UniqueID].ToString());
PageArgs e = new PageArgs();
e.CurPage = CurPage;
OnPageChange(this, e);
}
#endregion 事件
#region 私有属性
/// <summary>
/// 当前页
/// </summary>
private int CurPage
{
get
{
return (ViewState["CurPage"] == null) ? 1 : (int)ViewState["CurPage"];
}
set
{
ViewState["CurPage"] = value;
}
}
/// <summary>
/// 总页数
/// </summary>
private int PageCount
{
get
{
int _pageCount = 0;
if ((RecordCount % PageSize) == 0)
{
_pageCount = RecordCount / PageSize;
}
else
{
_pageCount = RecordCount / PageSize + 1;
}
return _pageCount;
}
}
#endregion 私有属性
#region 公有属性
/// <summary>
/// 记录数
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public int RecordCount
{
get
{
return (ViewState["RecordCount"] == null) ? 0 : (int)(ViewState["RecordCount"]);
}
set
{
ViewState["RecordCount"] = value;
}
}
/// <summary>
/// 页包含记录数
/// </summary>
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public int PageSize
{
get
{
return (ViewState["PageSize"] == null) ? 1 : (int)(ViewState["PageSize"]);
}
set
{
//EnsureChildControls();
ViewState["PageSize"] = value;
}
}
#endregion 公有属性
#region 方法
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string dopost = "<div>/n";
dopost += " <input type=/"hidden/" name=/"" + this.UniqueID + "/" id=/"__EVENTTARGET/" value=/"/" />/n";
dopost += "</div>/n";
dopost += "<script type=text/javascript>/n";
dopost += " function _doPost(cutomArg){/n";
dopost += " document.forms['form1']." + this.UniqueID + ".value = cutomArg;/n";
dopost += " document.forms['form1'].submit();/n";
dopost += " }/n";
dopost += "</script>/n";
if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "_doPost"))
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "_doPost", dopost);
}
}
protected override void RenderContents(HtmlTextWriter writer)
{
writer.Write("<div class=/"/">/n");
writer.Write("记录数:");
writer.Write(RecordCount);
writer.Write(" 总页数:");
writer.Write(PageCount + " ");
if (CurPage != 1)
{
writer.Write("<a href=/"javascript:_doPost('" + (CurPage - 1) + "')/">上一页</a>/n ");
}
for (int i = 1; i <= PageCount; i++)
{
if (i == CurPage)
{
writer.Write("<a href=/"javascript:_doPost('" + i + "')/">" + i + "</a>/n ");
}
else
{
writer.Write("<a href=/"javascript:_doPost('" + i + "')/">[" + i + "]</a>/n ");
}
}
if (CurPage != PageCount)
{
writer.Write("<a href=/"javascript:_doPost('" + (CurPage + 1) + "')/">下一页</a>/n");
}
writer.Write("</div>/n");
}
#endregion 方法
}
}