User Control Caching Issue

Discussion in 'ASP .Net' started by Jeff Schaefer, May 7, 2004.

  1. 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

    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
    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?

    Jeff Schaefer, May 7, 2004
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hypo
  2. Troy Simpson

    Fragment Caching inside page caching?

    Troy Simpson, Jan 19, 2004, in forum: ASP .Net
    Troy Simpson
    Jan 19, 2004
  3. Mad Scientist Jr
    Mad Scientist Jr
    Mar 22, 2006
  4. JimLad
    Jan 21, 2010
  5. Jonathan Wood

    User Control to Control Other User Control

    Jonathan Wood, Jan 24, 2010, in forum: ASP .Net
    Jonathan Wood
    Feb 2, 2010