Chrome Back button page refresh - ASP.net
Asked Answered
P

4

9

I have an ASP.net application (c#).

When a user is on a specific page, they click a link on this page that takes them to a child page, displaying the product details.

If the user clicks the browser back button, I need the parent page to be refreshed to its initial state. ie all text boxes that had data typed need to be blank, any hidden fields reset etc. Basically i need a CTRL-F5 when a user clicks back.

Disabling the back button is not an option.

I need this only on certain pages.

In IE and Firefox i can get this working without an issue. But with chrome the textboxes still contain their values as do the hidden fields. If I hit CTRL-F5 in Chrome, the page is correctly reset to its initial state.

This is the code I have tried.

<%@ OutputCache Location="None" VaryByParam="None" %>

and this:

   Response.Buffer = true;
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.Cache.SetAllowResponseInBrowserHistory(false);
   Response.Cache.SetNoStore();

and this:

    Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    Response.Cache.SetValidUntilExpires(false);
    Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetNoStore();

I have also tried a variety of these in different combination, but with no success.

thanks

Pak answered 29/4, 2010 at 12:48 Comment(1)
Which Chrome(s) did you try this with? Dev? Beta? Stable? Windows?Hen
E
6

When the browser's back button is pressed, the INPUT fields are not reset automatically by the browser. Instead, the browser retains the user's input, making it easier for users to go back and make a change to the input.

You cannot solve this server-side, because the browser bypasses the cache for this. Instead, you can use the autocomplete="off" HTML attribute on the input fields to prevent them from being retained by the browser.

You can also manually reset the form using JavaScript:

document.getElementById("form1").reset();
Echopraxia answered 29/4, 2010 at 12:57 Comment(3)
thanks. This doesn't explain why Ie & Firefox appear to be refreshing but not Chrome.Pak
It's a design choice by the Google folks I guess. I'm still glad they support he autocomplete attribute as a workaround.Echopraxia
This is a nasty "feature" of Chrome for devs. It took me ages to figure out what was happening to my dynamic form (users can add new rows which get saved to a db on submit). On "back", Chrome would not fill in the db ids of the previously added rows (even though my app was outputting it in the HTML) so my app thought all of them were new and duplicated them on a further submit. autocomplete="off" does the trick, however.Amby
P
2

These two lines solved the Chrome problem for me:

Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();

And I use this optional line to tell the browser not to create a history entry for each request of the same page:

Response.Cache.SetAllowResponseInBrowserHistory(true);

Sources:

Most popular answer in this post

Optional line

Periostitis answered 6/3, 2012 at 19:16 Comment(0)
G
0

The 'brute force' solution would be to put some javascript on the page that on page load sets the data to some known state. So it finds all the elements and sets the data based on an array of data or json object. On the initial request, since everything is defaulted anyways, the setting doesn't make a difference. On a back button request, since the javascript still has to be run, it resets all the values, regardless of browser.

I don't believe you can force a browser to function in the way you describe though, as it's up to each browser how they want to implement a back button - chrome just does it differently.

Glazed answered 29/4, 2010 at 12:57 Comment(0)
C
0

You can also put the autocomplete="off" on the form tag instead of every input field.

Cerallua answered 12/4, 2017 at 3:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.