3

I have a CheckBoxList like following

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        CheckBoxList1.Items.Add(new ListItem("Check/Uncheck All","0"));
        CheckBoxList1.Items.Add(new ListItem("A","1"));
        CheckBoxList1.Items.Add(new ListItem("B","2"));
        CheckBoxList1.Items.Add(new ListItem("C", "3"));
        CheckBoxList1.Items.Add(new ListItem("D", "4"));
    }        
}

I want whenever the first item is checked to check the rest of the items and whenever unchecked to uncheck the rest. Also the user can select every item separately.

I want do this with code behind without JavaScript or JQuery.

8 Answers 8

6

Try this

protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string result = Request.Form["__EVENTTARGET"];
    int index1 = int.Parse(result.Substring(result.IndexOf("$") + 1));
    if (index1 == 0)
    {
        bool tf = CheckBoxList1.Items[index1].Selected ? true : false;
        CheckUncheckAll(tf);
    }
}
void CheckUncheckAll(bool tf)
{
    foreach (ListItem item in CheckBoxList1.Items)
    {
        item.Selected = tf;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was hoping this would work for me, but this line ' int index1 = int.Parse(result.Substring(result.IndexOf("$") + 1' throws error that string is in the wrong format.
2
for(int index = 0; index < checkedListBox.Items.Count; ++index)
{
    checkedListBox.SetItemChecked(index, false);
}

Comments

1

There is a generic way of having a select all item in asp CheckBoxList with using jquery. You can have as many as CheckBoxList controls on the form with the select all functionality. you only need to make sure

  1. Your CheckBoxList has allowSelectAll Class
  2. You added a ListItem to your checkbox list to allow users to select All with the value of All

chkBoxList.Items.Insert(0, new ListItem("All", "All"));

you Only need the following code

<script>
    $('.allowSelectAll :checkbox[value=All]').click(function () {
        var toggle = this.checked;
        $(this).closest('.allowSelectAll').find(":checkbox").attr("checked", toggle);
    });
</script>

In the following code spinet I have 4 Checkbox lists

<div >
<label>Experience 1</label>
<asp:CheckBoxList ID="chklstExp1" runat="server"  CssClass="allowSelectAll">
</asp:CheckBoxList>

<label>Experience 2</label>
<asp:CheckBoxList ID="chklstExp2" runat="server" CssClass="allowSelectAll">
</asp:CheckBoxList>

<label>Experience 3</label>
<asp:CheckBoxList ID="chklstExp3" runat="server" CssClass="allowSelectAll">
</asp:CheckBoxList>

<label>Location</label>
<asp:CheckBoxList ID="chklstLocation" runat="server" CssClass="allowSelectAll">
</asp:CheckBoxList>

<asp:Button runat="server" ID="btnShowReport" OnClick="btnShowReport_Click" Text="Show Report"/>
</div>

Comments

1

With this piece of code, you could be able to access the checkbox from C# code behind and could be able to check/uncheck them or even enable/disable them. Hope it might help.

foreach (ListItem item in CheckBoxList.Items) {
    item.Selected = true;
    item.Enabled = true;
}

Comments

0

in .aspx page please add autopostback="true" for checkboxlist

then please add this event. Its working i have checked it.

Private Sub CheckBoxList1_SelectedIndexChsanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBoxList1.SelectedIndexChanged
    Dim result As String = Request.Form("__EVENTTARGET")
    Dim checkedBox As String() = result.Split("$"c)
    Dim index As Integer = Integer.Parse(checkedBox(checkedBox.Length - 1))
    If CheckBoxList1.Items(index).Text = "Check/Uncheck All" Then
        Dim Chkbool As Boolean = CheckBoxList1.Items(index).Selected
        For Each item In CheckBoxList1.Items
            item.selected = Chkbool
        Next
    End If
End Sub

Comments

0

For making items false you need to do:

checkList.ClearSelection();

For making items as true:

foreach (var item in checkList.Items.Cast<ListItem>().Where (li => li.Value == "1" || li.Value == "3" || li.Value == "5"))
{
   item.Selected = true;
}

Comments

0

for Check all

foreach (ListItem item in CheckBoxList.Items)
{
    item.Selected = true;    
}

for unchek all

 CheckBoxList.ClearSelection();

Comments

0

I have one radiobuttonlist and chechboxlist. checkboxlist's selection is changed automatically according to selection radiobuttonlist item selection. below are the image of output and code: hope it will help you.

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedValue == "0")
        {
            foreach (ListItem item in CheckBoxList1.Items)
            {
                item.Selected = true;
            }
        }
        if (RadioButtonList1.SelectedValue == "1")
        {
            CheckBoxList1.ClearSelection();

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.