Prevent rendering of user control

2.6k Views Asked by At

in our asp.net webforms application, we dynamically load usercontrols into placeholders. In order to retain changes across post-backs, our page-life-cycle is a little more complex than usual. We ALWAYS restore the previous control structure in pageInit in order to successfully load our viewstate. Only then do we clear the placeholder and load a new control into it.

This unfortunately means an entire life-cycle both for the old AND the new usercontrol, including server-side-processing of the old module's entire .ascx markup-file.

Now my question: Is there any possibility to minimize server-side processing of the old module, as it never gets sent back to the client (i.e. it's server-side rendering is completely unnecessary). What I'd ideally want to achieve is a sort of "light-weight" loading of a usercontrol, when it's only purpose is restoring vewstate information without it ever reaching the client.

The goal of the exercise is performance optimization.

Any hints, ideas or suggestions appreciated!

1

There are 1 best solutions below

1
On

I have resolved code running in webcontrol lifecycle events of dynamically added controls by simply checking if the control was visible (http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible.aspx)-

protected void Page_Load(object sender, EventArgs e)
{
    if (this.Visible) 
    {
       //Your code here
    }
}

If you have any methods that aren't triggered by a page lifecycle event, and have to be triggered by a user action, such as-

protected void Button1_Click(object sender, EventArgs e)
{
   //Do something
}

This can safely be left as is, the method code will not be run until the control is added to the page and the action is triggered.

Although the visibility check doesn't feel especially elegant, it's probably the best way to deal with auto wired events on dynamically loaded controls.