Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
h to all,
how to check and uncheck in checkbox list in asp.net

Design:-
XML
<asp:CheckBoxList ID="lst" runat="server" AutoPostBack="true" SelectionMode="Multiple" AppendDataBoundItems="true" DataTextField="BranchName" DataValueField="Branchcode" OnSelectedIndexChanged="lst_SelectedIndexChanged">
                              <asp:ListItem Text="All" Value="0" />
                          </asp:CheckBoxList>


And coding in c#:-
C#
protected void lst_SelectedIndexChanged(object sender, EventArgs e)
   {
       foreach (ListItem li in lstBranch.Items)
       {
           if (lstBranch.SelectedIndex == 0)
           {
               if (li.Text != "All")
               {
                   li.Selected = true;
               }
           }

       }
   }


above code if i tick "ALL" all the checkbox list is selected. if i untick "ALL" remaining checkbox should remain in tick but i need if i untick all then all want to untick if i tick all want to tick and multiple also multiple selection also required without tick "ALL"
Posted
Comments
Joan Magnet 20-Mar-15 7:04am    
Please try to clarify what you want. It's a bit confusing.
JOTHI KUMAR Member 10918227 20-Mar-15 7:26am    
simple if i tick "ALL" in list all the list item should be tick if i remove tick in "All" in list all the item should be in untick then without clicking "All" i need to select multiple selection also

For eg:
fruits like "ALL" list like 'apple','orange' like that ok
if i click fruits all lists want to tick,if i untick fruits all list want to untick ,if i want select only apple without tick fruits ok that all
F-ES Sitecore 20-Mar-15 7:42am    
You reference two checklists, lst and lstBranch, but you only mention one list if your explanation and markup.
ZurdoDev 20-Mar-15 8:33am    
Don't do it in C#. Google for javascript examples or jquery examples. There are tons of examples online.

1 solution

XML
<asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPostBack="true" SelectionMode="Multiple" AppendDataBoundItems="true" DataTextField="BranchName" DataValueField="Branchcode" OnSelectedIndexChanged="lst_SelectedIndexChanged_1">
     <asp:ListItem Text="All" Value="0" />
     </asp:CheckBoxList>
    <asp:CheckBoxList ID="lstMain" runat="server" AutoPostBack="true" SelectionMode="Multiple" AppendDataBoundItems="true" DataTextField="BranchName" DataValueField="Branchcode" OnSelectedIndexChanged="lst_SelectedIndexChanged">
    <asp:ListItem  Text="1" Value="0" />
    <asp:ListItem  Text="2" Value="0" />
    <asp:ListItem  Text="3" Value="0" />
    </asp:CheckBoxList>

C#
protected void lst_SelectedIndexChanged_1(object sender, EventArgs e)
      {
          if (CheckBoxList1.Items[0].Selected == true)
          {
              foreach (ListItem ls in lstMain.Items)
              {
                  ls.Selected = true;

              }

          }
          else
          {
              foreach (ListItem ls in lstMain.Items)
              {
                  ls.Selected = false;

              }

          }
      }
 
Share this answer
 
v3
Comments
balajidileepkumar 20-Mar-15 9:21am    
Work around is to add the all check box in a separate list
JOTHI KUMAR Member 10918227 20-Mar-15 9:39am    
hi balaji it works fine thanks for your reply thank you
balajidileepkumar 23-Mar-15 6:36am    
welcome
balajidileepkumar 23-Mar-15 6:49am    
any votes for me dear

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900