代码之家  ›  专栏  ›  技术社区  ›  Daan

为什么我必须显式地启用和禁用单选按钮?

  •  0
  • Daan  · 技术社区  · 16 年前

    当我想使用Ajax更改RadioButton的状态时,我发现有必要显式地设置每个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代码

    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 回复  |  直到 16 年前
        2
  •  2
  •   Paddy    16 年前

    如果需要这样做,可以使用RadioButtonList控件。