views:

325

answers:

2

Is it possible to let Visual Studio automatically create an event handler method for an UI component within the markup view?

Let's say I have

<asp:label runat="server" />

and would like to handle the OnPreRender event..

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

+1  A: 

You can automatically create a handler method by going to your page's OnLoad 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 kick in and offer to generate an event handler for you. If you hit TAB a couple of times, you should have a new MyLabel_PreRender method.

Good luck!

Lucas Richter
i am using vs 2005, so this helps .. dump, that i could not figure out myself :(
Michal
Not dumb at all. It's not possible to discover everything - that's why sites like this exist.
Lucas Richter
+1  A: 

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

Noldorin