ASP.Net RadioButton loses ViewState

Posted by Carl on Stack Overflow See other posts from Stack Overflow or by Carl
Published on 2010-04-13T19:42:32Z Indexed on 2010/04/13 19:53 UTC
Read the original article Hit count: 548

I'm having trouble with a simple radio set of two radio buttons (I don't want to use a RadioButtonList [RBL] because RBL doesn't allow child controls, and in my case, if you select one option, I want to enable a textbox next to the button; yes you could hack this with jQuery to move the textbox, but that's dirty!). I would check one, submit the form (either explicitly or through AutoPostBack), and the CheckedChanged event would never fire. When the page was reloaded, both buttons would be unchecked, regardless of their initial state on non-postback load or the state before form submission.

I took out the checkbox and stripped this down to the simplest example I could come up with. I tossed EnableViewState=true all over the place just in case it was being disabled somewhere I couldn't find.

<form id="form1" runat="server" enableviewstate="true">
<div>
        <asp:RadioButton ID="foo" Text="foo" runat="server" AutoPostBack="true" OnCheckedChanged="rbChanged" Checked="true" GroupName="foobar" EnableViewState="true" />
        <asp:RadioButton ID="bar" Text="bar" runat="server" AutoPostBack="true" GroupName="foobar"
            OnCheckedChanged="rbChanged" Checked="false" EnableViewState="true" />
        <asp:Label runat="server" ID="resultLbl" />
</div>
</form>

protected void rbChanged(object sender, EventArgs e)
    {
        if (foo.Checked) resultLbl.Text = "foo is checked";
        else if (bar.Checked) resultLbl.Text = "bar is checked";
        else resultLbl.Text = "neither is checked";
    }

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about radiobutton