Turn off page-level caching in a user control
Asked Answered
K

1

8

I have a page with the following caching defined:

<%@ OutputCache Duration="60" VaryByParam="None" %>

I have a user control inside that page that i don't want cached. How can I turn it off just for that control?

Kobe answered 23/7, 2010 at 13:32 Comment(0)
J
4

Option One

Use the Substitution control or API on your page. this enables you to cache everything on your page except the part contained within the substitution control.

http://msdn.microsoft.com/en-us/library/ms227429.aspx

One nice way to use this is to implement your control as a simple server control which renders the html as a string, but does so in the context of the page (that is with the correct Client IDs). Scott Guthrie has a really nice example of how this works. Works nicely with AJAX calls too by the way...

http://weblogs.asp.net/scottgu/archive/2006/10/22/Tip_2F00_Trick_3A00_-Cool-UI-Templating-Technique-to-use-with-ASP.NET-AJAX-for-non_2D00_UpdatePanel-scenarios.aspx

Excerpt from Scott Gu's article...

    [WebMethod]
    public string GetCustomersByCountry(string country)
    {
       CustomerCollection customers = DataContext.GetCustomersByCountry(country);

        if (customers.Count > 0)
            //RenderView returns the rendered HTML in the context of the callback
            return ViewManager.RenderView("customers.ascx", customers);
        else
            return ViewManager.RenderView("nocustomersfound.ascx");
    }

Option Two

Render the dynamic control via an AJAX call on the page load. This way, you can safely cache the entire page (including the AJAX call) and it is only the rendered result of the call that changes between pages.

Jospeh answered 23/7, 2010 at 13:36 Comment(5)
This works for fairly simple strings or html output but not if you need any kind of advanced rendering logic.Kobe
Can i render a user control from a substitution control?Kobe
Yes you can. See my update. It works for user controls as well.Jospeh
Approach #1 I was playing around with, but the problem is that i have controls that require a "form" tag, and that tag is located in the master page. Apparently rendering the control this way has no knowledge of the master page it's contained in because it's throwing errors looking for the "form" control.Kobe
Common issue with Substitution control: The Session is not available in the callback method. (Listed in Context instance, but is always null.)Cabot

© 2022 - 2024 — McMap. All rights reserved.