tags:

views:

138

answers:

4

Why is it that in .aspx pages all events are preceded with "On" e.g. "OnClick", "OnCommand" and in the code-behind file they are referred "Click", "Command"? Just Naming Convention or is there some logical explanation?

+2  A: 

Those methods are actually the methods called when the events are raised. Typically they will check if there are any subscribers. It has always been unclear to me why they are virtual.

Take a look at this code for the Button control.

protected virtual void OnClick(EventArgs e)
{
    EventHandler handler = (EventHandler) base.Events[EventClick];
    if (handler != null)
    {
        handler(this, e);
    }
}
ChaosPandion
This is not correct, or is referring to a different topic than what the OP is asking about. The "On[event]" property used in markup to assign a handler method by its name has nothing to do with the On[Event] method which raises that event. The "On-" convention is hard-wired into the parsing of the ASPX markup itself to tie events to handlers by their string name.
Rex M
It appears I misread the question.
ChaosPandion
@Rex M - Your comment is also an answer.
ChaosPandion
+1  A: 

AFAIK, just naming convention. They had to start with something :-) Prior to ASP.NET I think it was also like this in Windows applications and in JavaScript.

http://www.c-sharpcorner.com/UploadFile/puranindia/165/

http://webdevelopersjournal.com/articles/jsevents1/jsevents1.html

IrishChieftain
Just a name convention that follows VB style. Interesting that Delphi/VCL convention is just the opposite.
Lex Li
A: 

I may have your question wrong, but from what I can tell by what your asking, the EVENT and the PROPERTY cannot have the same name

The event is "Click"... example.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

But in the actual control, there is a property called "OnClick" whereby it activates the "Click" event. Therefor they cannot be named the same thing.

rockinthesixstring
+2  A: 

The names of the events themselves are Click, Change, etc... The internal methods to fire those events from code are prefixed with "On" as a naming convention. In ASP.NET markup, you use the attribute OnClick but what you're really doing is wiring a method to the "Click" event. Therefore, the method autogenerated for you by VS is ButtonName_Click. This method is internally passed as a delegate to the event itself.

Chris
On Click, do the Click Method. I guess one could called it `Button1_Clicked` method, for more accuracy.
Atømix