Does C# have a Handles keyword?

Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles Menu1.MenuItemClick


End Sub

In VB.net, we have the Handles keyword, I’m wondering if C# has anything comparable. Or do you have to manually wire all the methods to each control’s event (in ASP.NET especially)?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Nope. You will have to wire up the event like this

Menu1.MenuItemClick += Menu1_MenuItemClick;

Method 2

You have to assign the events yourself using the += syntax.

Method 3

Its pretty easy to setup handlers in C#. In my option much easier than VB.Net.
You’ll need to make sure the handler gets setup early enough in the page to get fired off.
Just type in the object name (Menu1) . (the name of the event) the “+=” and hit tab twice. Visual Stuido will generate everything for you.

Method 4

Generally this goes right in the markup.

<asp:Menu ID="Menu1" runat="server" onmenuitemclick="Menu1_MenuItemClick"></asp:Menu>

and in the codebehind it looks like

protected void Menu1_MenuItemClick(object sender, MenuEventArgs e)
{

}

You can generate all of this in Visual Studio from the designer. Select the menu control, go to the properties window (f4), view the events list (the lightning bolt) and double click the event name.

You can also subscribe an event handler manually with += but you have to do it every time the page loads.

Method 5

If you don’t mind doing so, you can always set the AutoEventWireUp to “true”. Then C# will automatically bind event handlers with “VB” names.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x