Tim
Tim

Reputation: 8921

Back button handling in ASP.NET

I've got several ASP.NET webforms in a "wizard-like" workflow:

form A is submitted, redirect to form B. Form B is submitted, redirect to Form C. Form C is submitted, redirect to Success.Aspx which tells the user the submission has succeeded and database has been updated.

At that point, the user should not be able to return to Form C and resubmit. However, if the user, on Success.aspx, clicks the browser's back button, no server-side event seems to fire on Form C. Page_Load does not fire again. Can Form C know that it is being revisited, and if so, how?

Or if Form C has know way of knowing that it is being revisited, is there some setting that can set in the server-side Submit-handler on Form C that would cause the page to expire and redirect user to Form A?

P.S. I've tried setting cache expiration in the Page_Load of my Form C but it did not cause the Page_Load to refire when revisiting the page using the Back button:

            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetExpires(DateTime.Now - new TimeSpan(1, 0, 0));
            Response.Cache.SetLastModified(DateTime.Now);
            Response.Cache.SetAllowResponseInBrowserHistory(false);

P.P.S. But this (from the link in the comment by @gh9) has the desired effect:

        Response.Cache.SetNoStore();
        Response.Cache.AppendCacheExtension("no-cache");
        Response.Expires = 0;

Upvotes: 1

Views: 4614

Answers (1)

Jack
Jack

Reputation: 44

I found this which might help you:

http://www.aspdotnet-suresh.com/2011/11/disable-browser-back-button.html

Author: Suresh Dasari

In the header of the page, place this JavaScript that should work in all browsers to prevent the user from reaching pages using the back button.

<script type="text/javascript" language="javascript">
function DisableBackButton() {
window.history.forward()
}
DisableBackButton();
window.onload = DisableBackButton;
window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
window.onunload = function() { void (0) }
</script>

The JavaScript will cause the browser to go forward as though the user hit the forward button in the browser.

You can also change the caching of the page so that it will display as expired if the user tries to go back to it.

Add the following to the top of the file:

using System.Web;

In Page_Init or Page_Load event method do the following:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetExpires(DateTime.Now.AddSeconds(-1));
Response.Cache.SetNoStore();

From experience, I can tell you the Javascript portion is a good fix to stop users from using the backbutton.

Upvotes: 1

Related Questions