Jump to content

ASP.Net with C# Codebehind - Event naming


Amb

Recommended Posts

I discovered something unusual doing some coding. Okay, perhaps it's not unusual, but it certainly wasn't anticipated by me.I wrote a server control in C#. I linked to that control in my ASP.net project. When I created the control I added some events to allow programmers of the ASP.net (or whatever) projects to override the standard functionality with custom functionality. When I hooked to the event (called CalculateACYR, see code blocks) in the ASP.net project CalculateACYR got underlined in visual studio as not being a valid property. After a quick use of intellisense merged with a modicum of intelligence (I only have a small amount to spread around :) ) I found that my event was now called "OnCalculateACYR". If I change the C# code to call the procedure XYZZY and sure enough the ASP.net code immediately refers to it as OnXYZZY.Component (C#)

public delegate void ContractCalculateACYR(Object sender, EventArgs e);public event ContractCalculateACYR CalculateACYR;

Client (ASP.net)

<cc1:StudentDeclaration id="StudentDeclaration1"  runat="server"  ... OnCalculateACYR="StudentDeclaration1_PrintACYR" />

Okay. This is predictable behaviour to me now. I really just want to know why this happens as this seems to fly in the face of the current Microsoft naming standard (eg a click event is just called click, and not onClick). I haven't managed to find any documentation on this feature or any form of explanation of when or why this happens.Thanks in advanceAmb

Link to comment
Share on other sites

The Click event is called Click and, as you mentioned, naming conventions would suggest that you name your event handler for the Click event of a button called "MyButton" as "MyButton_Click":

protected void MyButton_Click(object sender, EventArgs e)

However, in Intellisence, you'll see the event named "OnClick" and you hook that handler in the aspx as:

<asp:Button ID="MyButton" runat="server" OnClick="MyButton_Click" Text="CLICK ME!" />

I don't believe there is anything odd going on.

Link to comment
Share on other sites

The bit I find odd is that I am writing a custom event, and I called the custom event "CalculateACYR" but the control automatically seems to come through as "OnCalculateACYR".The example you give uses OnClick, which may or may not be defined in the .net library as just "Click" in the definition for the button object. The word On appears to be tacked on automatically. No your example isnt weird because it is using prebuilt events and as a programmer you wont care what the name is, just that you can use it. Does that make it clearer?

Link to comment
Share on other sites

Right, it doesn't matter if the event is called "Click", "CalculateACYR" or "RunAwayInAPanicAndHideInTheCloset".To programmatically attach an EventHander in the C#:

myAwesomeControl.RunAwayInAPanicAndHideInTheCloset += new EventHandler(FearHandler);

To add it in the ASPX:

<uc:MyAwesomeControl runat="server" id="myAwesomeControl" OnRunAwayInAPanicAndHideInTheCloset="FearHandler" />

Link to comment
Share on other sites

The event itself is called Click (or whatever you call yours). That is the *event*. The *event handler* is OnClick. The handler handles the event, so by default the handler has "On" plus the name of the event. The event handler and event are not the same thing, one is an event and one is a function to handle the outcome of the event.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...