User Control Caching Issue

J

Jeff Schaefer

What I want to know is this: Is it possible to programmatically manipulate a
custom property of a user control for which <%@ OutputCache ... > has been
included? If so, then how? My brief code is at the end of this message.

The Microsoft documentation is a bit confusing to me. The following numbered
sentences appear in the .NET Framework Developer's Guide article titled
"Caching Portions of an ASP.NET Page":

1. You can declare an ID attribute in a user control tag that you have
specified for output caching in order to program against that instance of
the user control. However, you must explicitly verify the existence of the
user control in the output cache in order for the code to work properly.

This makes me think it is possible - provided that you verify the existance
of the user control.

2. If you write code to manipulate a user control that contains an @
OutputCache directive, an error will occur.

This makes me think it is NOT possible.

3. A user control that is output cached is dynamically generated only for
the first request; any subsequent requests are satisfied from the output
cache until the specified time expires.

This seems to be the explanation for sentence #2 listed above; but then the
next sentence (#4 below) makes me think it's actually possible.

4. Once you have determined that the user control has been instantiated, you
can programmatically manipulate the user control from the containing page.
You can include this logic in one of the page lifecycle events associated
with the user control, such as in the Page_Load event or the Page_PreRender
event.

What I have is a user control that implements a custom property. Everything
worked great until I decided to cache the control. I really need to cache it
for runtime performance reasons, and I really need to set the property value
from the calling page. Here's my code that set the user control's custom
property (Orientation) from the page_load event of the hosting aspx page.

1 Control menuControl = LoadControl("_Menu01.ascx");
2 ((__Menu01)menuControl).Orientation = 1; // 1 means vertical, 0 means
horizontal
3 MenuPlaceHolder.Controls.Add(menuControl);

I get the following error for line 2: Specified cast is not valid.

If I remove the caching directive from the ascx file (<%@ OutputCache
Duration="1" VaryByParam="none" Shared="true" %>), then the page and control
loads just fine.

Any ideas?

Thanks!
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top