Search code examples
asp.netcheckboxlist

How to disable a particular CheckBoxList ListItem in ASP.NET?


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.


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;
       }
    }