how to write events using delegates in customcontrol开发者_如何学Go(Dropsownlist)
Here is sample using event in custom control:
using System;
using System.Web.UI;
namespace CustomControls
{
public class MyButton: Control, IPostBackEventHandler
{
// Defines the Click event.
public event EventHandler Click;
// OnClick dispatches the event to delegates that
// are registered with the Click event.
// Controls that derive from MyButton can handle the
// Click event by overriding OnClick
// instead of attaching a delegate. The event data
// is passed as an argument to this method.
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
{
Click(this, e);
}
}
// Method of IPostBackEventHandler that raises change events.
public void RaisePostBackEvent(string eventArgument)
{
OnClick(EventArgs.Empty);
}
protected override void Render(HtmlTextWriter output)
{
output.Write("<INPUT TYPE = submit name = " + this.UniqueID +
" Value = 'Click Me' />");
}
}
}
精彩评论