no postback with user control and OutputCache

Last post 01-10-2007 7:28 PM by dan cavina. 1 replies.

Sort Posts:

  • no postback with user control and OutputCache

    12-18-2006, 4:18 PM
    • Loading...
    • dan cavina
    • Joined on 08-25-2006, 4:56 PM
    • Posts 14

    I have a user control shared in many pages on my web site. Because it is created from a complicated database query, I decided to enable fragment caching for it.

    <%@ OutputCache Duration="600" VaryByParam="None" Shared="True" %>

    This works fine rendering-wise.

    However, I also have a textbox and an ImageButton in the same control. When a user enters text in the box and clicks the button, I need the control to do some action. The problem is that with fragment caching enabled for this control, I never see the postback in the control.

    On one hand, this makes sense since a cached control is not instantiated, but I would have thought there was some provision to allow standard ASP.NET behaviors to work. I can't find any documentation that mentions any restrictions on control or postback behavior when using fragment caching.

    I can think of the following workarounds:

    1) Break up the control into smaller sub-user controls so that the textbox and button are in a separate one that is not cached. The problem is that the containing structure of the control (i.e. outer divs) is not cached.

    2) While reading from the dabase is already cached, the rendering process is not. Perhaps I can create an in-memory representation of the rendered contents and just output that to a literal and leave the rest alone without caching. Again, this means that the containing structure of the control would not be cached.

    Perhaps there is some OutputCache or other setting that would allow me to make this work without coding changes. Any suggestions on workarounds? 

    Thanks in advance,

    Dan

     

  • Re: no postback with user control and OutputCache

    01-10-2007, 7:28 PM
    Answer
    • Loading...
    • dan cavina
    • Joined on 08-25-2006, 4:56 PM
    • Posts 14

    Well, to anybody that is interested...

    I ended up breaking up the main user control into one or more nested user controls. The parent does not do caching (sigh), while the nested ones that don't have any postback requirements turn it on as needed.

    Example: The main drop-down menu is created dynamically from a database query and contains only HTML links is cached to reduce rendering it more than once per minute. It's contained inside another user control that is the header for the site and includes a Search button and textbox.

    While working on this, I also discovered another issue that, on retrospect, makes perfect sense: you cannot use the Shared="True" attribute for controls that are used by pages in different folders if there are links or images defined with a relative path in the control. This is because the first one rendered in the cache would be wrong for pages in a different relative location. For now, I turned Shared . I could use absolute paths in all elements, except that it's a bit of a pain since the absolute paths are necessarily different between development and production servers.

    Cheers,

    Dan

Page 1 of 1 (2 items)
Microsoft Communities
Page view counter