Sunday, April 3, 2011

How come the attributes for event handler properties on ASP.NET controls have a prefix (OnLoad for the Load event handler)

This is just for a better understanding of the ASP.NET framework. When you use a control in a declarative way (that would be web form markup), you assign event handlers by their method name using an attribute that starts with On:

<asp:Button runat="server" OnClick="..."/>

But when you look at the System.Web.UI.WebControls.Button class it has an EventHandler property named Click that the delegate is assigned to:

button.Click += new EventHandler(...);

So how is this implemented? Is that just a convention followed by the parser?

I know, it's a strange question, the answer will do nothing but satisfy my curiosity.

From stackoverflow
  • OnClick is a method, that raises the control's Click event.

    Using the method, you can also handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

    See also the notes on Consuming Events (Event Handlers), and Raising Events (Methods).

0 comments:

Post a Comment