November Happy Hour will be moved to Thursday December 5th.

Disable caching for a specific page

Vote:
 

Hi,

In a page of mine that has some javascript things going on, I need to store a value in a html hidden input field.

The trouble is, this field seems to be caching, because when visiting the page, it's remembering the value from a previous visit!

Setting httpcacheexpiration to 0:00:00 in the web.config fixes it, but ideally I'd just like the value to not be cached. I'm setting it in the code-behind, which I'd imagine is why this is happening.

Is there any way I can stop it from caching, or can I disable caching on that specific page template?

Thanks for any help!

Karl. 

#33263
Oct 06, 2009 13:18
Vote:
 

Hi,

I'm not sure it will work in your specific case, but try using the ASP.NET Substitution Web Server Control. http://msdn.microsoft.com/en-us/library/ms228108.aspx

Leif 

#33265
Oct 06, 2009 13:47
Vote:
 

Hi,

I've investigated the substitution control, but as you said, it won't work with the problem I have. It uses a static method, and I'm actually trying to amend a hidden input field that's outside the control.

Does anybody else have any thoughts of how I can stop caching for a single hidden input field on a page short of setting httpcacheexpiration to 00:00:00?

Karl.

#33295
Oct 07, 2009 11:26
Vote:
 

Hi, 

You could add something like this to the Page_load of your template:

base.Response.Cache.SetExpires(DateTime.Now);
base.Response.Cache.SetCacheability(HttpCacheability.Private);

That will prevent all pages of that type to be cached.

//Morten

#33299
Oct 07, 2009 12:07
Vote:
 

Hey Morten!

I was looking on google for the last 2 hours to try and find something, and an article about setting httpcacheability to private for client-only caching in the web.config gave me a clue, but you just gave me the exact answer that I needed!

You are an absolute legend!

Another brief question then, if you would... Do I need to set the expiry of the cache to datetime.now for the private cacheability to take effect immediately, or is that just wiping out caching altogether, including client caching?

Thanks very much for your help so far, very much appreciated! :-)

Cheers,

Karl.

#33300
Oct 07, 2009 12:15
Vote:
 

Hi, 

Been awhile since I wrote that code so I had to do a little checking up and from I see HttpCachability.Private is default, and to be honest I do think you'd only need

base.Response.Cache.SetExpires(DateTime.Now);

Since that basically times out the chached version this instance no matter what HttpCacheability is set...

Maybe someone else here will verify that for us.

//Morten

#33301
Oct 07, 2009 12:25
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.