Output Caching for Dynamically Loaded User Controls

T

Teo Lachev

Hi,

How can I cache the output of user controls that are loaded dynamically
using LoadControl?

I have an aspx page which loads dynamically user controls under different
conditions as follows:

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
switch (ucName.ToUpper())
{
case "HOME":
ucName = "Home/Home.ascx"; break;
case "SERVICES":
ucName = "Services/Services.ascx"; break;
...
}
}

UserControl userControl = (UserControl) LoadControl(ucName);
placeHolder.Controls.Add(userControl);
}


I would like to cache the output of the controls using OutputCache directive
or programatically. Currently, if I enable output caching in the user
controls, only the fist one gets loaded (its Page_Load event executed). When
I request another control it doesn't get loaded and its Page_Load doesn't
fire. For example, when I request the page for the first time, the Home user
control gest successfully loaded, but when the page posts back and requests
"Services", the Services user control never gets shown. Instead the Home
user control output is shown.

I tried overriding the control UniqueID property and assign diffrent IDs for
the controls, as well as casting to PartialCachingControl, but it didn't
help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top