how to remove OutputCache programmatically for ascx?

asp.net, outputcache

Solution

Ok try this

In the page load of your user control put:

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

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

Change the key to whatever you want it to be for your control.

Then in the code of the event that you want to clear the cache put:

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

I saw this solution at http://dotnetslackers.com/ASP_NET/re-63091_ASP_NET_clear_user_control_output_cache.aspx

I tested it and it seems to work, although I do have to refresh the page once again after I call this like to see the updated control content.

Problem

I have a page1.aspx: ``` <%@ Register src="uc1.ascx" tagname="UcHead" tagprefix="uc1" %> ``` and uc1.ascx using the `OutputCache`: ``` <%@ OutputCache Duration="18000" VaryByParam="*" %> ``` How can I click a button in another page2.aspx to remove `OutputCache` for uc1.ascx or page1.aspx? When the OutputCache is in page1.aspx, I can use the following code to remove the OutputCache: ``` string url = "/page1.aspx"; HttpResponse.RemoveOutputCacheItem(url); ``` But it does not work when the OutputCache is in uc1.ascx.

Original source