Sometimes while clicking the Back browser button we would like the browser not to load the "cached" web page and get the fresh page from the server instead. This might be required in cases where we don't want to show user selected values in the previous page, or have some code in the previous page which we would like to run each time that page is accessed.
In ASP.NET 1.x, we can expire the response using the following code in the Page_Load():
/*
* Code disables caching by browser. Hence hitting the back browser button* causes the Page_Load event to fire again.
*/
Response.CacheControl = "no-cache";
Response.AddHeader("Pragma","no-cache");
Response.Expires = -1;
In ASP.NET 2.0, the Expires property is deprecated (included only for backwards compatibility) and Response.Cache should be used instead. See the code below:
Response.Cache.SetExpires(DateTime.Now);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
You can set it declaratively also by using the following in the ASPX page:
<%@ OutputCache Duration="0" VaryByParam="None" %>