关闭用户控件中的页面级缓存

Mic*_*cah 8 asp.net .net-4.0 donut-caching page-caching

我有一个定义了以下缓存的页面:

<%@ OutputCache Duration="60" VaryByParam="None" %>
Run Code Online (Sandbox Code Playgroud)

我在该页面中有一个用户控件,我不想缓存.如何才能为该控件关闭它?

Dan*_*son 4

选项一

使用页面上的替换控件或 API。这使您能够缓存页面上的所有内容(替换控件中包含的部分除外)。

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

使用此功能的一种好方法是将您的控件实现为一个简单的服务器控件,该控件将 html 呈现为字符串,但在页面的上下文中(即使用正确的客户端 ID)执行此操作。Scott Guthrie 有一个非常好的例子来说明它是如何工作的。顺便说一句,与 AJAX 调用也能很好地配合......

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

摘自 Scott Gu 的文章...

    [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");
    }
Run Code Online (Sandbox Code Playgroud)

选项二

通过页面加载时的 AJAX 调用呈现动态控件。这样,您就可以安全地缓存整个页面(包括 AJAX 调用),并且只有调用的渲染结果在页面之间发生变化。