注意 此版本的 Visual Basic .NET 不支持事件属性。
[C#]
using System;
using System.Web.UI;
namespace CustomControls
{
public class OptimizedEventButton: Control, IPostBackEventHandler
{
// Defines a key for storing the delegate for the Click event
// in the Events list.
private static readonly object ClickEvent = new object();
// Defines the Click event using the event property syntax.
// The Events property stores all the event delegates of
// a control as name/value pairs.
public event EventHandler Click
{
// When a user attaches an event handler to the Click event
// (Click += myHandler;), the Add method
// adds the handler to the
// delegate for the Click event (keyed by ClickEvent
// in the Events list).
add
{
Events.AddHandler(ClickEvent, value);
}
// When a user removes an event handler from the Click event
// (Click -= myHandler;), the Remove method
// removes the handler from the
// delegate for the Click event (keyed by ClickEvent
// in the Events list).
remove
{
Events.RemoveHandler(ClickEvent, value);
}
}
// Invokes delegates registered with the Click event.
//
protected virtual void OnClick(EventArgs e)
{
// Retrieves the event delegate for the Click event
// from the Events property (which stores
// the control's event delegates). You must
// cast the retrieved delegate to the type of your
// event delegate.
EventHandler clickEventDelegate = (EventHandler)Events[ClickEvent];
if (clickEventDelegate != null) {
clickEventDelegate(this, e);
}
}
// Method of IPostBackEventHandler that raises change events.
//
public void RaisePostBackEvent(string eventArgument)
{