原创代码,翻页自定义控件
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized ;
using System.ComponentModel;
using System.Web;
namespace Lee
{
/// <summary>
/// 自动维护所有的翻页参数
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:pageMake runat=server></{0}:pageMake>")]
public class pageMake : System.Web.UI.WebControls.WebControl , System.Web.UI.IPostBackDataHandler, System.Web.UI.IPostBackEventHandler
{
private string text;
//private int currenPage ;
//private int totalPage ;
private string name="pageMake";
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}
set
{
text = value;
}
}
[Bindable(true),
Category("Appearance"),
DefaultValue("1")]
public int CurrenPage
{
get
{
return (int)ViewState["currenPage"+this.name];
}
set
{
ViewState["currenPage"+this.name] = value;
}
}
public int TotalPage
{
get
{
return (int)ViewState["totalPage"+this.name];
}
set
{
ViewState["totalPage"+this.name] = value;
}
}
public int TotalRows
{
get
{
return (int)ViewState["totalPage"+this.name];
}
set
{
double a;
a= (double)value;
a = a/this.PageSize;
ViewState["totalPage"+this.name] = (int)System.Math.Ceiling(a);
}
}
public int PageSize
{
get
{
return (int)ViewState["PageSize"+this.name];
}
set
{
ViewState["PageSize"+this.name] = value;
}
}
public string SQLString
{
get
{
return (string)ViewState["SQLString"+this.name];
}
set
{
ViewState["SQLString"+this.name] = value;
}
}
public int PageStart
{
get
{
return this.PageSize*this.CurrenPage;
}
}
[Bindable(true),
Category("Appearance"),
DefaultValue("1")]
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
//if (this.CurrenPage > 0)
output.Write("<table style='display:block' border=0><tr><td style='cursor:hand' onclick=javascript:" + Page.GetPostBackEventReference(this,"TopPage") + ">首页</td>");
if (this.CurrenPage > 0)
output.Write("<td style='cursor:hand' onclick=javascript:" + Page.GetPostBackEventReference(this,"PreviousPage") + ">上一页</td>");
else
output.Write("<td style='cursor:default'>上一页</td>");
if (this.CurrenPage+1 < this.TotalPage)
output.Write("<td style='cursor:hand' onclick=javascript:" + Page.GetPostBackEventReference(this,"NextPage") + ">下一页</td>");
else
output.Write("<td style='cursor:defalut'>下一页</td>");
output.Write("<td style='cursor:hand' onclick=javascript:" + Page.GetPostBackEventReference(this,"BottomPage") + ">最后页</td>");
//output.Write("<td style='cursor:hand' onclick=javascript:" + Page.GetPostBackEventReference(this,"BottomPage") + ">最后页</td>");
output.Write("<td>每页 " + this.PageSize + " 条, 共 " + ViewState["totalPage"+this.name] + " 页, 第 "+ ( (int)ViewState["currenPage"+this.name] + 1) +" 页</td></tr></table>");
}
public virtual bool LoadPostData(string postDataKey,
NameValueCollection postCollection)
{
// String presentValue = Text;
// String postedValue = postCollection[postDataKey];
//
// if (presentValue == null || !presentValue.Equals(postedValue))
// {
// Text = postedValue;
// return true;
// }
return false;
}
public virtual void RaisePostDataChangedEvent()
{
;
}
// Defines the Click event.
public event EventHandler NextPage;
public event EventHandler PreviousPage;
public event EventHandler AbsPage;
public event EventHandler TopPage;
public event EventHandler BottomPage;
// Define the method of IPostBackEventHandler that raises change events.
public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument == "NextPage")
{
//currenPage ++;
ViewState["currenPage"+this.name] = ((int)ViewState["currenPage"+this.name])+1;
NextPage(this,new EventArgs());
}
if (eventArgument == "PreviousPage")
{
ViewState["currenPage"+this.name] = ((int)ViewState["currenPage"+this.name])-1;
PreviousPage(this,new EventArgs());
}
if (eventArgument == "AbsPage")
{
ViewState["currenPage"+this.name] = ((int)ViewState["currenPage"+this.name])+1; //从消息中得到的参数来决定
AbsPage(this,new EventArgs());
}
if (eventArgument == "TopPage")
{
ViewState["currenPage"+this.name] = 0;
TopPage(this,new EventArgs());
}
if (eventArgument == "BottomPage")
{
ViewState["currenPage"+this.name] = (int)ViewState["totalPage"+this.name] -1;
BottomPage(this,new EventArgs());
}
}
}
}