views:

44

answers:

2

When I want to change the state of radiobuttons using AJAX, I discovered that it is necessary to set the state of each radiobutton explicitly, rather than just setting Checked = true on the one that you want to have enabled. If I remove the marked lines below, I can perhaps change the radiobuttons state once or twice using the buttons, but no more.

Can anyone explain why this is? It feels redundant to have to explicitly uncheck the other radiobuttons than the one I want to check.

ASP Code

<div>
    <asp:UpdatePanel ID="UP_Checkboxes" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <asp:RadioButton ID="RadioButton1" runat="server" GroupName="MyRBs" /><br />
            <asp:RadioButton ID="RadioButton2" runat="server" GroupName="MyRBs" /><br />
            <asp:RadioButton ID="RadioButton3" runat="server" GroupName="MyRBs" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>
<div>
    <asp:UpdatePanel ID="UP_Buttons" runat="server" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:Button ID="Button1" runat="server" Text="Select 1" OnClick="Button1_Click" /><br />
            <asp:Button ID="Button2" runat="server" Text="Select 2" OnClick="Button2_Click" /><br />
            <asp:Button ID="Button3" runat="server" Text="Select 3" OnClick="Button3_Click" />
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

CS Code

protected void Button1_Click(object sender, EventArgs e)
{
    RadioButton1.Checked = true;
    RadioButton2.Checked = false; // Removing these lines causes problems
    RadioButton3.Checked = false; // Removing these lines causes problems
    UP_Checkboxes.Update();
}

protected void Button2_Click(object sender, EventArgs e)
{
    RadioButton1.Checked = false; // Removing these lines causes problems
    RadioButton2.Checked = true;
    RadioButton3.Checked = false; // Removing these lines causes problems
    UP_Checkboxes.Update();
}

protected void Button3_Click(object sender, EventArgs e)
{
    RadioButton1.Checked = false; // Removing these lines causes problems
    RadioButton2.Checked = false; // Removing these lines causes problems
    RadioButton3.Checked = true;
    UP_Checkboxes.Update();
}
+2  A: 

You could use a RadioButtonList control, if you need to do this sort of thing.

Paddy