Checkboxlist  Select All checkbox

Posted on 2009-07-02
Last Modified: 2012-05-07
I have a checkboxlist and want a select all checkbox. The way I want it to work is that if they hit select all it checks all the boxes. The problem I am running into is that if they have 'item1' checked and you click "select all". I cant tell that select all is checked because in the selectedindexchanged event it is still showing me a value of item1.

How can I get this to work right? I basically want Select all to check all the boxes and if they have select all checked and uncheck any checkbox it unchecks select all.

    <form id="form1" runat="server">


        <asp:ScriptManager ID="ScriptManager1" runat="server">


        <asp:UpdatePanel ID="UpdatePanel1" runat="server">


        <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="2" AutoPostBack="true">






            <asp:ListItem>Select All</asp:ListItem>








Open in new window

Question by:bertino12
  • 2
  • 2

Expert Comment

ID: 24765246
Use a for loop... checking for all controls that are checkboxes. If you have some you don't want checked, then use a groupbox as a container for the checkboxes... now you will use two nested for loops.
Dim ctl As Object


        For Each ctl In Lookup.Controls

            if ctl is checkbox then

            'yourCheckbox.checked = true


Open in new window

LVL 41

Accepted Solution

guru_sami earned 500 total points
ID: 24765271
Try this:
Protected Sub CheckBoxList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    If CheckBoxList1.Items(5).Selected = True Then
        For Each item As ListItem In CheckBoxList1.Items
            item.Selected = True
    End If
End Sub

This code has its own limitations .eg. Items(5) this index 5 may change depending on the number of checkboxes. But it should give you an idea where to head.

Author Comment

ID: 24766208
Could it be done through javascript?

Can I wire a javascript function to the  checkboxlist items so that when they are clicked it fires a javascript function that if false unchecks the "All" option and if true checks all the checkboxes?

How could I do this?
LVL 41

Expert Comment

ID: 24766411
May be you can try Adding onchange attribute to CBL OnPageLoad. Not sure how would that go...but because using a single checkbox to Select ALL and Deselect ALL would create a problem.
Say someone wants to select just one that time SelectAll will not be selected ..that will make delect all implied if using a singel Checkbox.

Check using two buttons like this:

Author Comment

ID: 24766996
I ended up doing it in javascript:

    Private Sub addAttributes()

        Me.cbLaws.Items(11).Attributes.Add("style", "color: red")

        For i As Integer = 0 To 11

            If i = 11 Then

                Me.cbLaws.Items(i).Attributes.Add("onclick", "javascript:Select(true)")


                Me.cbLaws.Items(i).Attributes.Add("onclick", "javascript:Select(false)")

            End If


    End Sub

<script language="javascript"  type="text/javascript"> 

function Select(flag){

    if(flag==true) {  

        if(document.getElementById("ctl00_PageBody_cbLaws_11").checked == true) {

            var i=0;

            for (i=0;i<=11;i++)


                document.getElementById("ctl00_PageBody_cbLaws_" + i).checked = true;



        if(document.getElementById("ctl00_PageBody_cbLaws_11").checked == false) {

            var i=0;

            for (i=0;i<=11;i++)


                document.getElementById("ctl00_PageBody_cbLaws_" + i).checked = false;






        document.getElementById("ctl00_PageBody_cbLaws_11").checked = false;




Open in new window


Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now