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

时间:2009-10-09 14:34:20

标签: asp.net outputcache

我有一个page1.aspx:

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

和uc1.ascx使用OutputCache

<%@ 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 个答案:

答案 0 :(得分: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

看到了这个解决方案

我测试了它似乎工作,虽然我在调用它之后必须再次刷新页面,以便查看更新的控件内容。

相关问题