Does C# have a Handles keyword?

两盒软妹~` 提交于 2019-12-01 17:32:38

问题


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)?


回答1:


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

Menu1.MenuItemClick += Menu1_MenuItemClick;



回答2:


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.




回答3:


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




回答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.




回答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.



来源:https://stackoverflow.com/questions/6130834/does-c-sharp-have-a-handles-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!