3

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.

Oleks
  • 31,955
  • 11
  • 77
  • 132
Mike108
  • 2,089
  • 7
  • 34
  • 45

1 Answers1

5

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.

Chris Mullins
  • 6,677
  • 2
  • 31
  • 40
  • It works When the OutputCache is in page1.aspx. But it does not work when the OutputCache is in uc1.ascx. – Mike108 Oct 09 '09 at 15:16
  • I updated my answer see if that works. I had never tried to output cache user controls before so I am interested in how to do it as well. – Chris Mullins Oct 09 '09 at 16:51
  • It's great! Thank you very much! And my small tip: the code should be put at the end of the page_load of UC after gridview binding, otherwise a gridview does not refresh outputcache correctly. – Mike108 Oct 09 '09 at 18:33