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)?
Nope. You will have to wire up the event like this
Menu1.MenuItemClick += Menu1_MenuItemClick;
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.
You have to assign the events yourself using the +=
syntax.
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.
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.
来源:https://stackoverflow.com/questions/6130834/does-c-sharp-have-a-handles-keyword