2009-10-09 19 views
3

我有一個page1.aspx這個:如何以編程方式刪除OutputCache for ascx?

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

和uc1.ascx使用OutputCache

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

我怎樣才能在另一個page2.aspx點擊一個按鈕來刪除OutputCache爲uc1.ascx或page1.aspx這個?

當的OutputCache是​​page1.aspx這個,我可以使用下面的代碼刪除的OutputCache:

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

但當的OutputCache是​​uc1.ascx這是行不通的。

回答

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

這種解決方案我測試,它似乎工作,雖然我有在我稱之爲看到更新後的控件內容後再次刷新頁面。

+0

它的工作原理OutputCache位於page1.aspx中時。但是,當OutputCache位於uc1.ascx時它不起作用。 – Mike108 2009-10-09 15:16:35

+0

我更新了我的答案,看看是否有效。我從來沒有試過輸出緩存用戶控件,所以我對如何做這件事感興趣。 – 2009-10-09 16:51:03

+0

太棒了!非常感謝你!我的小建議是:在gridview綁定之後,代碼應該放在UC的page_load的末尾,否則gridview不會正確刷新outputcache。 – Mike108 2009-10-09 18:33:37