Wiring Events of ASCX Controls after ParseControl

After you loaded your control definition from the ascx and retrieved the embedded controls using FindControl(), you notice that the child controls’ events do not fire anymore.

While the Page_Load and other Page_ events fire correctly for the control that loaded the ascx, the events for the controls inside the ascx are ignored: You need to wire them explicitly in code.

If, for example, your ascx contains a button declaration

<asp:Button ID="btnCalc" runat="server" OnClick="btnCalc_Click"  />

you need to retrieve the control and wire the event like this

btnCalc = (Button)this.FindControl("btnCalc");
btnCalc.Click += btnCalc_Click;

One Response to Wiring Events of ASCX Controls after ParseControl

  1. […] Wiring Events of ASCX Controls after ParseControl […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: