user1951370
user1951370

Reputation: 29

Web browser back button should go back to specific page

I have a file selection menu, the user selects a file and a new page loads the clicked file in a text area. The user edits the text in the text area and clicks a save button.

Then the user should be able to go back to the file selection menu by clicking the web browsers back button.

The problem: The web browser requires two clicks on the web browsers back button. It should only require one click.

Question: How can I make the browser go back to the menu by only one click on the web browsers back button?


EDIT: I am using sessions:

protected void Page_Load(object sender, EventArgs e)
        {
    if (Session["BrowserBackButton"].ToString() == "true")
                    {
                        Response.Redirect("MainMenu.aspx", true);
                    }
}

Problem: When I write to the text area and then click browser back button one time it won't load the server side code again, it just removes the previously written text. Another click and it loads the server again.

Can I load the server on browser back button click?

Upvotes: 1

Views: 5738

Answers (3)

evilom
evilom

Reputation: 583

You could use sessions to check whether the loaded page (from back button) was fired backwards.
Then redirect the page to your desired page.

Something like this...

page 1 > page 2

page 2 onload

if (Session["Back"] != null) //if fired backward using the back button
  //redirect to desired page
  //don't forget to set Session["Back"] back to null

page 2 > page 3

page 3 onload

Session["Back"] = true; //or some key that will determine that you have already reached this page

On the first load of page 2 it will be normal since Session["Back"] is empty.
But when you use the back button to return to page 2..Session["Back"] will have a value that will allow you to do anything like a page redirect to page 1.

Upvotes: 0

GHP
GHP

Reputation: 3231

Simple. Make the "Save" button fire an AJAX call to do the saving of the edit, instead of a normal Postback. Show the user a 'success' message once the AJAX call is resolved, and then when they hit the Back button, they will go back 1 step to the File Selection page.

Post your existing code if you need a hand with the AJAX. There's a lot of options on how to do it.

Upvotes: 0

Abhitalks
Abhitalks

Reputation: 28437

How can I make the browser go back to the menu by only one click on the web browsers back button?

No you can't. You cannot change browser's behaviour or functionality.

However, you can show a link which goes back to 2 levels in history. Something like this:

<a href="#" onclick="history.go(-2);">Back</a>

Or better still:

<a href="./originalPage.aspx">Back</a>

Just give a link to the original page itself.

Upvotes: 1

Related Questions