为什么我必须明确启用和禁用radiobutton?

时间:2010-10-20 14:46:08

标签: c# asp.net ajax asp.net-ajax

当我想使用AJAX改变radiobutton的状态时,我发现有必要明确设置每个单选按钮的状态,而不是仅在你想要启用的那个上设置Checked = true。如果我删除下面标记的行,我可以使用按钮更改一次或两次radiobuttons状态,但不能更新。

任何人都能解释为什么会这样吗?必须明确取消选中其他无线电按钮而不是我要检查的无线电按钮是多余的。

ASP代码

<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 个答案:

答案 0 :(得分:2)

如果您需要执行此类操作,可以使用RadioButtonList控件。

答案 1 :(得分:2)