如果启用了另一个复选框,如何禁用复选框?

问题描述 投票:-1回答:2

我的网页有两个复选框,我想要的是:

  • 最初,两个复选框都未选中。
  • 如果选中Checkbox1,Checkbox2将被禁用。
  • 如果再次取消选中Checkbox2,Checkbox2将再次启用。
  • 反之亦然。

请帮我修改我的代码。

这是我的代码:

protected void CheckBox1_CheckedChanged1(object sender, EventArgs e)
{
   //this.CheckBox1.CheckedChanged += new System.EventHandler(CheckBox1_CheckedChanged1);
    if (CheckBox1.Checked)
        CheckBox2.Enabled = false;
}

protected void CheckBox2_CheckedChanged2(object sender, EventArgs e)
{
    if (CheckBox2.Checked)
        CheckBox1.Enabled = false;
}

HTML

<asp:CheckBox ID="CheckBox1" runat="server" Height="33px" OnCheckedChanged="CheckBox1_CheckedChanged1" Font-Bold="True" style="margin-left: 33px" Text="Remove Blank Lines" TextAlign="Left" Width="162px" />


<asp:CheckBox ID="CheckBox2" runat="server" Font-Bold="True" Height="33px" OnCheckedChanged="CheckBox2_CheckedChanged2" style="margin-left: 28px" Text="Add Prefix/ Suffix to Blank Lines" TextAlign="Left" Width="259px" />
c# html checkbox webforms
2个回答
0
投票

只需使用:

CheckBox2.Enabled = !CheckBox1.Checked;

0
投票

这个答案是对新撰稿人的澄清

AutoPostBack属性添加到每个复选框控件

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack ="True" OnCheckedChanged="CheckBox_CheckedChanged" Height="33px"  Font-Bold="True" style="margin-left: 33px"  Text="Remove Blank Lines" TextAlign="Left" Width="162px" />


<asp:CheckBox ID="CheckBox2" runat="server" AutoPostBack ="True" OnCheckedChanged="CheckBox_CheckedChanged" Font-Bold="True" Height="33px"  style="margin-left: 28px"   Text="Add Prefix/ Suffix to Blank Lines" TextAlign="Left" Width="259px" />

然后在代码隐藏

创建一个事件CheckBox_CheckedChanged并指向每个checkbox

protected void CheckBox_CheckedChanged(object sender, EventArgs e)
  {
     CheckBox1.Enabled = !CheckBox2.Checked;
     CheckBox2.Enabled = !CheckBox1.Checked;
  }
© www.soinside.com 2019 - 2024. All rights reserved.