Automatically create event handler from markup view (C #)

Can Visual Studio automatically create an event handler method for a UI component in markup view?

Let's say I have

<asp:label runat="server" />

      

and would like to handle the OnPreRender event.

How do you create a handler method? Manually or do you switch to design view and double-click the event in the properties window?

0


a source to share


3 answers


You can automatically create a handler method by going to the OnLoad page or Page_Load method and adding a handler for the event. For example, for this label:

<asp:label ID="MyLabel" runat="server" />

      

You would do this:



protected void OnLoad(object sender, EventArgs e)
{
     MyLabel.PreRender += 
}

      

At this point, IntelliSense should start and suggest generating an event handler for you. If you hit TAB twice, you should have a new method called MyLabel_PreRender.

Good luck!

+1


a source


You should simply write an event handler in the markup view and use tab completion to generate the method in code and specify it in the markup at the same time. This feature is new to VS.NET 2008, I believe, so if you are using a previous version, you may not have this feature.



+1


a source


Take a look at this msdn link: http://msdn.microsoft.com/en-us/library/6w2tb12s%28v=VS.90%29.aspx (VS 2008 version)

It says that you can create a method declaratively named Page_event.

For example, to create a handler for the page load event, create a method named Page_Load.

ASP.NET pages automatically bind page events to methods that are named Page_event. This autobinding is configured by the AutoEventWireup attribute in the @Page directive, which is set to true by default. If AutoEventWireup is set to false, the page will not automatically look for methods that use the Page_event naming convention.

Worked for me!

+1


a source







All Articles