ASP.NET服务器控件已经有Button,而我写的这个控件仅仅实现提交功能
Code
[DefaultEvent("Click")]
public class RosanshaoButton:WebControl,IPostBackEventHandler
{
private static readonly object ClickKey = new object();
public RosanshaoButton()
: base(HtmlTextWriterTag.Input)
{ }
public event EventHandler Click
{
add
{
Events.AddHandler(ClickKey, value);
}
remove
{
Events.RemoveHandler(ClickKey, value);
}
}
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
Page.VerifyRenderingInServerForm(this); //确保此控件在form表单内
}
writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
writer.AddAttribute("Name", this.UniqueID);
base.Render(writer);
}
IPostBackEventHandler 成员#region IPostBackEventHandler 成员
protected virtual void OnClick(EventArgs e)
{
EventHandler clickEventDel = Events[ClickKey] as EventHandler;
if (clickEventDel != null)
{
clickEventDel(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
OnClick(EventArgs.Empty);
}
#endregion
}
通过此控件的开发,明白了IPostBackEventHandler接口.IPostBackEventHandler定义了ASP.NET 服务器控件为处理回发事件而必须实现的方法。若要创建从浏览器捕获窗体提交信息的服务器控件,必须实现此接口。IPostBackEventHanlder是针对事件处理,如button。它就是实现此接口。
[DefaultEvent("Click")]
public class RosanshaoButton:WebControl,IPostBackEventHandler
{
private static readonly object ClickKey = new object();
public RosanshaoButton()
: base(HtmlTextWriterTag.Input)
{ }
public event EventHandler Click
{
add
{
Events.AddHandler(ClickKey, value);
}
remove
{
Events.RemoveHandler(ClickKey, value);
}
}
protected override void Render(HtmlTextWriter writer)
{
if (Page != null)
{
Page.VerifyRenderingInServerForm(this); //确保此控件在form表单内
}
writer.AddAttribute(HtmlTextWriterAttribute.Type, "submit");
writer.AddAttribute("Name", this.UniqueID);
base.Render(writer);
}
IPostBackEventHandler 成员#region IPostBackEventHandler 成员
protected virtual void OnClick(EventArgs e)
{
EventHandler clickEventDel = Events[ClickKey] as EventHandler;
if (clickEventDel != null)
{
clickEventDel(this, e);
}
}
public void RaisePostBackEvent(string eventArgument)
{
OnClick(EventArgs.Empty);
}
#endregion
}
通过此控件的开发,明白了IPostBackEventHandler接口.IPostBackEventHandler定义了ASP.NET 服务器控件为处理回发事件而必须实现的方法。若要创建从浏览器捕获窗体提交信息的服务器控件,必须实现此接口。IPostBackEventHanlder是针对事件处理,如button。它就是实现此接口。