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

Mik*_*108 3 asp.net outputcache

我有一个page1.aspx:

<%@ Register src="uc1.ascx" tagname="UcHead" tagprefix="uc1" %>
Run Code Online (Sandbox Code Playgroud)

和uc1.ascx使用OutputCache:

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

如何单击另一个page2.aspx中的按钮以删除OutputCacheuc1.ascx或page1.aspx?

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

string url = "/page1.aspx"; 
HttpResponse.RemoveOutputCacheItem(url); 
Run Code Online (Sandbox Code Playgroud)

但是当OutputCache在uc1.ascx中时它不起作用.

Chr*_*ins 5

好的,试试吧

在您的用户控件的页面加载中:

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

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

将密钥更改为您希望控制的任何密钥.

然后在要清除缓存的事件代码中:

Cache.Insert("myCacheKey", DateTime.Now);
Run Code Online (Sandbox Code Playgroud)

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

我测试了它似乎工作,虽然我打电话给这个看起来更新的控件内容后我必须再次刷新页面.