views:

454

answers:

3

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();
        }
    }
}
A: 

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:

http://stackoverflow.com/questions/158319/cross-browser-onload-event-and-the-back-button/201406

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

SolutionYogi
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
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
I've tried this both with and without Response.Cache.SetCacheability(HttpCacheability.NoCache) set
Kirschstein
SolutionYogi
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
Edited my original answer.
SolutionYogi
A: 

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
A: 

I am not currently familiar with .net but wishing to get in touch in .. thanks for sharing nice post

webguy