Click here to Skip to main content
15,884,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
ASP.NET
<pre lang="c#">
<asp:CheckBoxList ID="Student" class="checkS" align="left" runat="server" 
              onselectedindexchanged="Student_SelectedIndexChanged" AutoPostBack="true" >
                <asp:ListItem Text="Insert"   Value="Insert" ></asp:ListItem>                
                <asp:ListItem Text="Update" Value="Update"></asp:ListItem>
                <asp:ListItem Text="View"   Value="View"></asp:ListItem>
                <asp:ListItem Text="All"    Value="Delete"></asp:ListItem>
            </asp:CheckBoxList>



C#
protected void Student_SelectedIndexChanged(object sender, EventArgs e)
  {

      foreach (ListItem li in Student.Items)
      {
          // li.Selected = true;
          if (li.Text == "All" && li.Selected == true)
          {
              foreach (ListItem li1 in Student.Items)
              {
                  li1.Selected = true;
              }
          }

          if ((li.Text == "Insert" && li.Selected == true)|| (li.Text == "Update" && li.Selected == true))
          {
              Student.Items.FindByText("View").Selected = true;

          }

      }

  }


What I have tried:

1)If we check all==> all checkboxes are selected
2)uncheck all==>all are unchecked
3)if select insert/update view also should select by default view for insert and update
Posted
Updated 2-Aug-18 20:05pm
Comments
Vincent Maverick Durano 3-Aug-18 12:23pm    
I'm looking at your " What I have tried" but I'm struggling to understand what you really want to do? Does your existing code does not work?

1 solution

I suggest use of jquery to do selecting and unselecting of the checkboxes because 1. It will not result to a postback, 2. You can use checkboxList class -checks- to select all / unselect all.
Example:
$("input[type='checkbox']").change(function () {
if (checkboxname=="checkall") {
if (this.checked) {
$(".checks").prop("checked", true);
}
else {
$(".checks").prop("checked", false);
}
}
if (checkboxname=="checkupdate") {
if (this.checked) {
$(".checks").prop("#checkupdate", true);
$(".checks").prop("#checkview", true);
}
else {
$(".checks").prop("checked", false);
}
}
});
 
Share this answer
 

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