?
Solved

Checkboxlist  Select All checkbox

Posted on 2009-07-02
5
Medium Priority
?
693 Views
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.
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
 
        <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="2" AutoPostBack="true">
            <asp:ListItem>item1</asp:ListItem>
            <asp:ListItem>Item2</asp:ListItem>
            <asp:ListItem>Item3</asp:ListItem>
            <asp:ListItem>Item4</asp:ListItem>
            <asp:ListItem>Item5</asp:ListItem>
            <asp:ListItem>Select All</asp:ListItem>
        </asp:CheckBoxList>
        
        </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>

Open in new window

0
Comment
Question by:bertino12
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:mpeaden2
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
        Next

Open in new window

0
 
LVL 41

Accepted Solution

by:
guru_sami earned 2000 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
        Next
    End If
End Sub

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

Author Comment

by:bertino12
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?
0
 
LVL 41

Expert Comment

by:guru_sami
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 checkbox...at that time SelectAll will not be selected ..that will make delect all implied if using a singel Checkbox.

Check using two buttons like this: http://www.dotnetcurry.com/ShowArticle.aspx?ID=77
0
 

Author Comment

by:bertino12
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)")
            Else
                Me.cbLaws.Items(i).Attributes.Add("onclick", "javascript:Select(false)")
            End If
        Next
    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;
            }       
        }
    } 
    if(flag==false)
    {
        document.getElementById("ctl00_PageBody_cbLaws_11").checked = false;
    }
}
</script>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

777 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