tags:

views:

15

answers:

1

Suppose I have a user control MyControl.ascx, and I put it in the Default.aspx like this:

<uc1:MyControl id="MyControl" runat="server">

Now in the code-behind of Default.aspx I do this:

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    MyControl.Visible = false;
}

The problem is that even I do not render MyControl, it is still being initialised and goes through the entire ASCX life cycle (OnLoad, etc). Is there a way to prevent the control from being initialised at all?

Thanks.

+1  A: 

Instead of adding it to aspx better add it to a Placeholder from code behind dynamically. This would allow you to initialise it when you require.

Happy coding.

Ravia