views:

192

answers:

5

I'm trying to fully understand the WebForm event model (not the page lifecycle, but how the events are wired up when specified declaratively in the .aspx or .ascx files.

Take the Button control for example. It has a Click event that you can wire to in the code-behind, but it has an "OnClick" event in the .aspx/.ascx file.

I used the .NET Reflector and the Button control has a PROTECTED OnClick method, but that shouldn't be available to be assigned by the .aspx/.ascx. Unless I'm missing something.

Does anyone know why the "On" prefix is added?

Just to clarify a bit: I understand the naming convention works. I'd like to know how the "OnClick" in the .aspx/.ascx gets translated into .Click += new EventHandler(blahName); I.e. if I create a ControlChanged EventHandler, do I need to do anything special to get the OnControlChanged to show up validly in the .aspx/.ascx file?

+6  A: 

Those store references to the delegates that the calling code will be wiring up using events; in order to distinguish between the event itself, and the delegate.

Jason Watts
Yes because "Click" sounds like it might be causing a click, whereas "OnClick" is caused by a click.
ChrisW
+2  A: 

It's just a naming convention used when raising events. OnSomethingHappened ... OnClick, OnChange, OnClose. I don't think there is anything magical or sinister, it's just a convention.

JP Alioto
A: 

Semantically it is basically an old throwback to VB traditions where event listeners were generally called OnWhatever. Old habits die hard.

Wyatt Barnett
A: 

It's more than a naming convention because events in user controls automatically get the "On" prefix in the declarative syntax.

For example, I have a UserControl that declares a ProjectSelected event. To add a handler declaratively, I set the OnProjectSelected attribute.

UserControl:

        public event EventHandler<ProjectSelectedEventArgs> ProjectSelected;

Adding handler declaratively:

        <user:ProjectList id="uxProjectList" runat="server"
            OnProjectSelected="uxProjectList_ProjectSelected" />

Adding handler in code behind:

        uxProjectList.ProjectSelected += uxProjectList_ProjectSelected;

This confused the hell out of me twice, once when I couldn't figure out why the event wasn't available declaratively, and again when I named the event "OnProjectSelected" and the attribute became "OnOnProjectSelected".

Jamie Ide
It's actually a little deeper than conventions used by the Visual Studio IDE. Events vs delegates: http://blog.monstuff.com/archives/000040.html
Jason Watts
@Jason: I didn't find that article to be clear at all. The event handler for an event is defined by a delegate. You register handlers with an event that conform to the event's handler definition. To make it easier to handle events, some classes handle their own events that call an overrideable "On[Event]" method so that consumers can override the method instead of handling the event. ASP.NET appears to do this automatically to allow the declarative syntax.
Jamie Ide
Apologies, it was the first article I pulled off google to illustrate the difference between events and delegates, since I felt that wasn't addressed in your post (as it should be to make the "On" convention clearer)
Jason Watts