Instead of
calling the event directly, a good design pattern followed by all the prebuilt server controls in
ASP.NET is to add a virtual protected method that invokes the event with a prefix of On attached to
the name of the method. This provides an additional level of abstraction that allows controls
that derive from a base control to easily override the event-raising mechanism to run additional
business logic or suppress event invocation altogether. The following code shows an OnClick
protected method used to provide access to the Click event of class:
protected virtual void OnClick(EventArgs e)
{
if (Click != null)
Click(this, e);
}
The first thing the protected method does is check to see if any client methods have registered
themselves with the Click event instance. The event field will have a null value if no
clients have registered a method onto the delegate??™s invocation list. If clients have subscribed
to the Click event with a method having a matching signature, the event field will contain an
object reference to a delegate that maintains the invocation list of all registered delegates. The
OnClick routine next invokes the event using the function call syntax along with the name of
the event. The parameters passed in are a reference to the control raising the event and the
event arguments passed into the routine.
Pages:
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291