C# – Automatically create event handler from markup view (c#)

cvisual studio

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?

Best Answer

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!

Related Topic