我有一个页面,其中定义了以下缓存:
<%@ OutputCache Duration="60" VaryByParam="None" %>
我有一个用户控件在页面中,但我不希望它被缓存。如何只针对该控件关闭缓存?
我有一个页面,其中定义了以下缓存:
<%@ OutputCache Duration="60" VaryByParam="None" %>
我有一个用户控件在页面中,但我不希望它被缓存。如何只针对该控件关闭缓存?
选项一
在您的页面上使用替换控件或API。这使您可以缓存页面上除替换控件包含的部分之外的所有内容。
http://msdn.microsoft.com/en-us/library/ms227429.aspx
一个好的方法是将您的控件实现为简单的服务器控件,将HTML呈现为字符串,但在页面上下文中执行(即使用正确的客户端ID)。Scott Guthrie有一个非常好的例子说明了这个方法。顺便说一下,这也可以很好地与AJAX调用配合使用...
摘自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");
}
选项二
通过页面加载时的 AJAX 调用来渲染动态控件。这样,您可以安全地缓存整个页面(包括 AJAX 调用),只有调用的渲染结果在页面之间发生变化。