Monday, November 7, 2011

Disable caching (Browser back button problem)


When the user hit the back button a locally cached copy of the previous page is retrieved without notifying the server. As a result the Info is out of sync.

Solution 1 --> Disable caching (do not work for all browsers)
Code to disable caching (to be executed everytime thepage is loaded)

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(Now.AddSeconds(-1));   //--- (-1) means the page already expired
Response.Cache.SetNoStore();  //--tell the page not to cache the page
Response.AppendHeader("Pragma", "no-cache");  //---adds a header to the HTTP response object


Solution 2 --> Use timestamps or random numbers to track pages and detect when a page is current