Question

Let's say we have two pages: pageX.aspx, and pageY.aspx. These pages utilize a master page along the top of the browser, where the user has several options to change the language of the site. These language choices are in the form of image buttons (country flags), but they do not utilize the PostbackUrl property. Instead they just use event handlers. One such image handler:

protected void imgbtnEnglish_Click(object sender, ImageClickEventArgs e)
{
    HttpCookie aCookie = new HttpCookie("language");
    aCookie.Value = "en-US";
    aCookie.Expires = DateTime.Now.AddDays(365);
    Response.Cookies.Add(aCookie);

    Response.Redirect(Request.RawUrl);
}

pageX.aspx contains an image button, which has the property PostbackUrl="pageY.aspx". The sole purpose of this image button is to just take us to pageY.aspx.

When that image button is pressed, as expected, we are taken to pageY.aspx.

Now here's the mystery to me: when I press BACK in the browser (not a control or anything, just the good old back button), I go back to pageX.aspx, which is great!...except, seemingly, the master page at the top thinks I'm still at pageY.aspx! Because even though I'm now back at pageX.aspx on my screen, clicking any of the flags at the top take me back to pageY.aspx!

The only thing I can logically deduce is that, for some reason, since it's a PostbackUrl, and it submits the form to the server, going "back" is only recognized locally for the form that was sent, NOT the entire page (including the master page). So therefore by going "back", I'm only actually going "back" in only the form, and not the form AND master page.

What I did to solve my problem, is to make the image button that takes me to pageY.aspx not use a PostbackUrl, but instead a Click event handler with one line: Response.Redirect("pageY.aspx"); When I do this, it works fine.

I want to understand why this happens, and also if the Click event handler solution is the best practice/proper solution instead of using a PostbackUrl property.

Was it helpful?

Solution

When the back button is pressed browsers restore the page from the cache together with the latest form data e.g. text boxes retain their entered values. However, Opera tries to be a bit smarter and restores the latest state of the JavaScript as well.

Setting a PostBackUrl means that, when the button is clicked, the form action is changed using JavaScript and the form posted to this new URL. So, when you press back in Opera it restores the page together with the JavaScript change to the form action, which means all subsequent submits will go to this new URL.

I'm not a fan of using the PostBackUrl because it only works with JavaScript enabled. Using Response.Redirect server side and Hyperlinks client side are a better solution.

OTHER TIPS

PostbackUrl vs NavigateUrl

Basically with NavigateURL, you are doing a POST. Response.Redirect would do a GET. I'm not totally sure of why you're seeing that particular behaviour, but it could very well be your browser. What browser are you using? What other browser(s) have you tried?

As for what would be the best way to do this, I think you're better off forgetting the ASP.NET controls and doing this:

<a href="pageY.aspx"><img src="foo.png" /></a>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top