views:

608

answers:

4

Consider the scenario:

  1. I visited a page of a website built using ASP.NET. The page is a simple aspx page containing ASP.NET server controls.

  2. I clicked on a link which takes me to some other page on the same website.

  3. I clicked the BACK button of the browser.

QUESTION: What happens in terms of page life cycle? Does all the events occur or the browser just displays the cached version of the page without making any requests?

A: 

usually all the events should occur, but if you have an uber browser than it could happen to display a cached page you can just put a breakpoint in your Page Load and see if it's going to occur

Omu
A: 

The page would be displayed from Cache.

Ravia
So if I have Response.Cache.SetCacheability(HttpCacheability.NoCache) set, would hitting Back cause the browser to issue a Get request for the provious page, or maybe even a postback if that was the last action of the user?
AaronLS
yes it would postback if you have Response.Cache.SetCacheability(NoCache) set, but for some pages you will see the same content.Try using Response.Cache.SetCacheability(HttpCacheability.NoCache);Response.Cache.SetExpires(DateTime.Now-new TimeSpan(1,0,0));Response.Cache.SetLastModified(DateTime.Now);Response.Cache.SetAllowResponseInBrowserHistory(false);
Ravia
I think you would get a "Page Expired" notice instead of the page, but I think it depends on the browser still.
Codesleuth
+5  A: 

I think the best answer is: It depends on the browser, especially after a post/postback.

Older browsers used to pop up a confirmation dialog to the effect of "the page contains POST data which will be resubmitted", and you could either proceed (resubmit) or cancel out. Since everything that happens in ASP.NET WebForms is part of the FORM element (ViewState, events, etc.), this would cause the entire lifecycle to be repeated.

Of course, this caused no end of trouble with duplicate submissions, so many sites had to come up with workarounds for the dupe problem, and today most browsers just fetch the page from cache instead.

...That is unless you override the cache-control headers and force the browser not to store the page in cache. Obviously, in that case, it can't be retrieved from cache, so it will usually end up being resubmitted. But, again, it depends on the browser - for example, some browsers won't allow the resubmission over SSL, so if that's the protocol in use then the user will just see a message saying that the page has expired / can't be shown.

Come to think of it, probably an even better answer is: As a site designer, you really can't depend on any specific behaviour from the user's browser when the Back button is clicked. If a duplicate submission could have negative side-effects (such as charging a credit card twice), then you need to take adequate measures to prevent that from happening. It's good practice anyway as it's entirely possible for a user to simply double-click the "submit" button by accident.

Aaronaught
I agree with Aaronaught's answer. I would not try to write any code which assumes that the 'back' button will behave a certain way across all browsers.
jessegavin
I think you considered a different scenario of post-backing the same page using a control and then pressing the BACK button...Am I right?
Manish
@Manish: What matters is not necessarily whether or not the user's *present* page has `POST` data, but whether or not the *previous* page (the one that the Back button will take them to) had `POST` data. That includes both scenarios - going back from a postback, and going back from a new page when the previous page had a postback (or just `POST`).
Aaronaught
A: 

we have even tried

Response.ExpiresAbsolute = DateTime.Parse("1/1/1980");
Response.AddHeader("cache-control", "no-store, must-revalidate, private");
Response.AddHeader("Pragma", "no-cache");

to resolve this kind of problem

Ravia