如何以编程方式删除ascx的OutputCache?

3

我有一个page1.aspx页面:

<%@ Register src="uc1.ascx" tagname="UcHead" tagprefix="uc1" %>

使用OutputCache缓存uc1.ascx:

<%@ OutputCache Duration="18000" VaryByParam="*"  %> 

如何在另一个page2.aspx页面上点击按钮以移除uc1.ascx或page1.aspx的OutputCache?

当OutputCache在page1.aspx中时,我可以使用以下代码来移除OutputCache:

string url = "/page1.aspx"; 
HttpResponse.RemoveOutputCacheItem(url); 

但是当OutputCache在uc1.ascx中时,它无法正常工作。

1个回答

5

好的,请尝试以下操作:

在用户控件的页面加载中添加以下代码:

HttpRuntime.Cache.Insert("myCacheKey", DateTime.Now);

BasePartialCachingControl pcc = Parent as BasePartialCachingControl;
pcc.Dependency = new CacheDependency(null, new string[]{"myCacheKey"});

将键更改为您控件所需的任何内容。

然后在您想要清除缓存的事件代码中添加:

Cache.Insert("myCacheKey", DateTime.Now);

我在http://dotnetslackers.com/ASP_NET/re-63091_ASP_NET_clear_user_control_output_cache.aspx看到了这个解决方案。

我测试过了,它似乎可以工作,不过在调用此方法后,我需要再次刷新页面才能看到更新后的控件内容。


当OutputCache在page1.aspx中时,它是有效的。但当OutputCache在uc1.ascx中时,它就无效了。 - Mike108
我更新了我的答案,请看它是否有效。我以前从未尝试过缓存用户控件的输出,因此我也很感兴趣如何做到这一点。 - Chris Mullins
太好了!非常感谢!我还有一个小提示:代码应该放在UC的page_load结束后,gridview绑定之后,否则gridview无法正确刷新outputcache。 - Mike108

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接