0

I have a webforms page that makes quite extensive use of AJAX. There are a number of links on it that take the user off to a different page. I want to maintain the state that the page was in should the user come back to it using the browser's back button.

How I've gone about this is to store the page's state in session each time the user interacts with the page. When they return to the page, if a state exists in session, it is loaded and displayed to the user.

I have this "sort of" working. The page is saved correctly, and can be loaded correctly, but unfortunately requires a page refresh (F5) to load it, otherwise it falls back to a previous state.

Any idea why the page is not being rendered correctly without a refresh? How can I go about solving this?

protected void Page_Load(object sender, EventArgs e)
{
    Response.Cache.SetCacheability(HttpCacheability.NoCache);

    if (!Page.IsPostBack)
    {
        DataBindDropDowns();

        if (HasQueryString())
        {
            SetOptionsFromQueryString();
            SavePageToSession();
            Response.Redirect("Default.aspx");
        }
        else
        {
            RestoreFromSession();
            DisplayOptions();
        }
    }
}
Kirschstein
  • 14,570
  • 14
  • 61
  • 79

3 Answers3

0

Are you using the 'load' event to initialize the page? When a user presses the back button, the 'load' event doesn't fire but the 'dom content ready' event does.

See:

Is there a cross-browser onload event when clicking the back button?

If you are using jQuery, try to put your page initialization logic in ready event and you should be good. If you are not using jQuery, find the appropriate way to hook up to the DOM Content Loaded/Ready event and use it to initialize your page.

If it still doesn't work, please post your code so that we can debug the problem.

EDIT:

Looks like you want to set appropriate Expires header so that browser will reload the page when back button is clicked. Try code from following page:

http://www.352media.com/rantingandraving/rave.aspx?raveid=325&id=212

Community
  • 1
  • 1
SolutionYogi
  • 31,807
  • 12
  • 70
  • 78
  • Editted my question to show code. I'm doing this from the .NET code behind, not javascript. Sorry, should have made that more clear. – Kirschstein Jul 13 '09 at 16:05
  • Well, depending upon your page expiration setting, browser may or may not call the server side page when user clicks on the back button. If you have not setup any page expiration setting in your code, then it will have a default expiration and when user presses the back button, browser will not request the page from server, it will show the cached copy and your Load event won't fire. – SolutionYogi Jul 13 '09 at 16:12
  • I've tried this both with and without Response.Cache.SetCacheability(HttpCacheability.NoCache) set – Kirschstein Jul 13 '09 at 16:22
  • 1
    Try using Expires header. Example: http://www.352media.com/rantingandraving/rave.aspx?raveid=325&id=212 – SolutionYogi Jul 13 '09 at 16:45
  • That link sovles my problem, thanks. Do you want to edit your post to add this and I'll mark this as the answer. – Kirschstein Jul 13 '09 at 17:06
0

Here is my guess based off the Issue of PageLoad not firing...

I bet the .aspx file is being sent to the client with cache headers...

Pull up Fire Bug or Fiddler and see it MaxAge http response header is coming from you server.

If so, that is what you need to get removed as the browser is not re-requesting the file cause it was told not to.

BigBlondeViking
  • 3,853
  • 1
  • 32
  • 28
0

HTML 5 offline storage is another option and quite amazing because it works in all major browsers since IE 8 came out.

I did a jQuery plugin for saving form state for returning users at http://www.jasonsebring.com/dumbFormState that accomplishes much of what you are talking about and has nothing to do with server-side and even works in HTML pages.

There are some more features I could add such as saving the form in its entirety so it would not even need to be rendered on an additional page hit. I'm focusing on saving the actual choices the user made automatically then when the user comes back, the form selections are still where they left off and you can erase them from session or local storage as you want through the form instance. This is a brave new world going into HTML 5 and there will be many old paradigms that will vanish as the newer browsers erase the past problems. Maintaining form state is certainly a huge and old problem that all of use would like to go away.

JavaScript is only getting stronger these days so you should get those HTML 5 skills going. I recommend reading, listening and watching everything from Douglas Crockford as your skepticism in taking JavaScript seriously will evaporate and you will find that closures are the best thing ever created in programming besides variables and functions.

King Friday
  • 25,132
  • 12
  • 90
  • 84