Disable Caching on an ASP.Net Page
I have been working on database driven ASP.Net site lately and have been having problems with the browser caching pages variables. This is causing issues when the user uses the back command to navigate to the previous page instead of using the provided command button to return to the page they need.
In order to prevent the page from being cached and force it to be loaded as a new page each time, add the following code to the Page_Load event :
VB.Net:
Response.Buffer = True Response.ExpiresAbsolute = Now().Subtract(New TimeSpan(1, 0, 0, 0)) Response.Expires = 0 Response.CacheControl = "no-cache"
C#:
Response.Buffer = true; Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0)); Response.Expires = 0; Response.CacheControl = "no-cache";
So simple, yet so effective.
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.




Comments
No comments yet.
Leave a comment