Wednesday, August 13, 2014

_doPostBack and Browser back button behavior



The odd behavior of Browser Back Button on _doPostBack()



 In one of application developed in Asp.net page is not behaving as desired when browser back button is pressed.

The steps performed in the following order

1) Go to the page
2) Click a link which causes _doPostBack and navigate to new page
3) Press the browser back button
4)click on any button 

This will end up re-executing the _doPostBack() action instead of firing Button click event.

The reason or behavior is as follows
  • Back button goes back to the previous page - page is not reloaded from server!
  • Browser assigns __EVENTxxx etc. with the previous POST back values
  • The server detects the __EVENTxxxx form vars and routes events accordingly (incorrect behavior)
To overcome this behavior i had a simple solution in my mind to expire the page and empty the secure page cache.
It worked for me by introducing following code in page load event and use is at the top not in any checks like IsPostback etc.


Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetAllowResponseInBrowserHistory(false);