Question

In my web page, there are two checkboxlists.

<asp:CheckBoxList ID="chk1" runat="server" AutoPostBack="True" onselectedindexchanged="chk1_SelectedIndexChanged" >
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
</asp:CheckBoxList>

and

<asp:CheckBoxList ID="ch2" runat="server" AutoPostBack="True" >
    <asp:ListItem Value="3">3</asp:ListItem>
    <asp:ListItem Value="4">4</asp:ListItem>
</asp:CheckBoxList>

If I checked 1 in chk1, 3 should be disabled in chk2, and if I checked 2 in chk1, 4 should be disabled in chk4.

Was it helpful?

Solution

Try this,

protected void chk1_SelectedIndexChanged(object sender, EventArgs e)
{
   if(chk1.selectedIndex==0)
   {
      chk2.Items[0].enabled=false;
   }
    else if(chk1.selectedIndex==1)
   {
      chk2.Items[1].enabled=false;
   }
}

OTHER TIPS

Please try with the below code snippet.

protected void chk1_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (ListItem item in chk1.Items)
    {
        switch (item.Value)
        {
            case "1":
                if (chk2.Items.FindByValue("3") != null && item.Selected == true)
                    chk2.Items.FindByValue("3").Enabled = false;
                else
                    chk2.Items.FindByValue("3").Enabled = true;
                break;
            case "2":
                if (chk2.Items.FindByValue("4") != null && item.Selected == true)
                    chk2.Items.FindByValue("4").Enabled = false;
                else
                    chk2.Items.FindByValue("4").Enabled = true;
                break;
            default:
                break;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top