views:

156

answers:

1

I have an ASP.NET page with two radio buttons. The buttons use an auto-postback to run server side logic. If you select the second button, the page posts back and correctly displays a message that the second button is selected. If the browser back button is now clicked, the state of button 2 stays checked while the message reverts to saying that button one is checked. This results in a situation that is obviously undesirable because the state of the radio buttons and message are now out of sync. I know this is a caching issue but I find it odd that the browser remembers the previous state of the label but not the previous state of the radio button.

What is the best way to handle this situation? It's undesirable in this situation to disable cache or the use of the back button.

The following code example exhibits this behavior:

[Form:]

    <asp:RadioButton ID="rb1" runat="server" AutoPostBack="true" Text="Button1" OnCheckedChanged="rb_CheckedChanged"
        GroupName="rbgroup" Checked="true" />
    <br />
    <asp:RadioButton ID="rb2" runat="server" AutoPostBack="true" Text="Button2" OnCheckedChanged="rb_CheckedChanged"
        GroupName="rbgroup" />
    <br />
    <hr />
    <asp:Label ID="lbl1" runat="server">Button 1</asp:Label>

[Code Behind:]

protected void rb_CheckedChanged(object sender, EventArgs e)
{
    if (rb1.Checked == true)
        lbl1.Text = "Button 1";
    else
        lbl1.Text = "Button 2";
}
A: 

Hmm - that is funny.

I can reproduce this. And what surprises me is that the source (view source of browser) shows the correct radio button checked (Button 1). The browser (I tested IE and FF) does not seem to render the source correctly (or not redraw the radio button; or is not showing the correct source).

Now the thing with the back button is a bit tricky. Mainly because there is not much you can do about it... HTTP is and will stay stateless and the back button on the client is "far away" from your web server. You cannot control it.

As an article about a similar topic you could read A Thorough Examination of "Disabling the Back Button".

What I am trying to say is that although I do not understand why the browser seems to be rendering something different from what the source tells it, I fear you are fighting a losing battle here.

The only thing I can currently think of would be to find some smart JavaScript that I am unable to provide that "does something" to make the browser redraw the screen after the user clicked the back button.

scherand