ASP.NET Radio Button State and Back Key

Posted by Dave S on Stack Overflow See other posts from Stack Overflow or by Dave S
Published on 2010-05-04T19:53:30Z Indexed on 2010/05/04 19:58 UTC
Read the original article Hit count: 160

Filed under:

When using the browser back key on a page with radio buttons and using auto-postback, the state of the buttons (checked) stays the same while other parts of the page do not. This results in a situation where button two is checked and the cached page (because of server-side logic) displays a message that button 1 is checked.

What is the best way to handle this situation?

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";

}

© Stack Overflow or respective owner

Related posts about ASP.NET