一个有Onclick事件的button例子:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
namespace ComponentControl
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyButton runat=server></{0}:MyButton>")]
public class Ctrl4 : Control, IPostBackEventHandler
{
public event EventHandler Click;//定义事件,EventHandler为系统委托名
IPostBackEventHandler 成员IPostBackEventHandler 成员IPostBackEventHandler 成员
public string Text //设置属性Text的值
{
get { return ViewState["text"] == null ? "Button" : ViewState["text"].ToString(); }
set { ViewState["text"] = value; }
}
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Type, "Submit");
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID); //跟下面一句代码实现同样效果,触发服务器端事件,属性必须是Name,不能为ID
//writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference(this));
writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
//writer.Write("<INPUT TYPE=submit name=" + this.UniqueID +" Value='Button' />");
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
namespace ComponentControl
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:MyButton runat=server></{0}:MyButton>")]
public class Ctrl4 : Control, IPostBackEventHandler
{
public event EventHandler Click;//定义事件,EventHandler为系统委托名
IPostBackEventHandler 成员IPostBackEventHandler 成员IPostBackEventHandler 成员
public string Text //设置属性Text的值
{
get { return ViewState["text"] == null ? "Button" : ViewState["text"].ToString(); }
set { ViewState["text"] = value; }
}
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Type, "Submit");
writer.AddAttribute(HtmlTextWriterAttribute.Name, this.UniqueID); //跟下面一句代码实现同样效果,触发服务器端事件,属性必须是Name,不能为ID
//writer.AddAttribute(HtmlTextWriterAttribute.Onclick, Page.GetPostBackEventReference(this));
writer.AddAttribute(HtmlTextWriterAttribute.Value, Text);
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.RenderEndTag();
//writer.Write("<INPUT TYPE=submit name=" + this.UniqueID +" Value='Button' />");
}
}
}
另外一种优化的事件实现
EventHandlerList 类提供一个简单的委托列表来添加和删除委托,下面来看看更改后的代码,
AddHandler有两个参数事件对象和添加的委托,在OnClick事件中必须显示将委托转换为EventHandler类型
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
namespace ComponentControl
{
public class Ctrl5:Control,IPostBackEventHandler
{
// 声明Click事件委托
private static readonly object obj = new object();
public virtual event EventHandler Click
{
add {
Events.AddHandler(obj, value);
}
remove
{
Events.RemoveHandler(obj, value);
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<input type=submit name="+this.UniqueID+" value=Button />");
}
IPostBackEventHandler 成员IPostBackEventHandler 成员
}
}
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
namespace ComponentControl
{
public class Ctrl5:Control,IPostBackEventHandler
{
// 声明Click事件委托
private static readonly object obj = new object();
public virtual event EventHandler Click
{
add {
Events.AddHandler(obj, value);
}
remove
{
Events.RemoveHandler(obj, value);
}
}
protected override void Render(HtmlTextWriter writer)
{
writer.Write("<input type=submit name="+this.UniqueID+" value=Button />");
}
IPostBackEventHandler 成员IPostBackEventHandler 成员
}
}