0

My web-page have two check-boxes and What I want is:

  • Initially both check-boxes are unchecked.
  • if Checkbox1 is checked, Checkbox2 should becomes disabled.
  • if Checkbox2 is unchecked again, Checkbox2 should becomes enabled again.
  • and vice versa.

Please help me in modifying my code.

Here is my code:

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" />
Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
Emmy
  • 145
  • 11

2 Answers2

0

Just use:

CheckBox2.Enabled = !CheckBox1.Checked;
Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • @Emmy, I think you have to write above condition for `CheckBox1` also like => `CheckBox1.Enabled = !CheckBox2.Checked;` in `CheckBox2_CheckedChanged2` event handler. – er-sho Mar 22 '19 at 06:32
  • 3
    Probably @Emmy assumes this is client-side code while it is actually server-side code that either requires `AutoPostBack="true"` or a submit button. Maybe provide a client-side-only solution, too? – Uwe Keim Mar 22 '19 at 06:34
0

This answer for clarification to the new contributor

Add The AutoPostBack property to each checkbox control

<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" />

then in code-behind

create one event CheckBox_CheckedChanged and point each checkbox at it:

protected void CheckBox_CheckedChanged(object sender, EventArgs e)
  {
     CheckBox1.Enabled = !CheckBox2.Checked;
     CheckBox2.Enabled = !CheckBox1.Checked;
  }
Anas Alweish
  • 2,818
  • 4
  • 30
  • 44