Link to home
Start Free TrialLog in
Avatar of cdlciddit
cdlciddit

asked on

How can I tell if any of the checkboxes in a <asp:CheckBoxList> are checked

Hello. I have a <asp:CheckBoxList> with 4 ListItems.  I would like to know how I can tell which if any of those checkboxes are checked.  To be more specific I would like to know if the other box is checked and if so a panel should stay visible.  So far this is what I have:

<asp:CheckBoxList ID="cblEduAssistPlans" runat="server" SelectedIndexChanged="cblEduAssistPlans_SelectedIndexChanged" AutoPostBack="true">
                       <asp:ListItem>Individualized education program (IEP)</asp:ListItem>
                       <asp:ListItem>Individualized Family Service Plan (IFSP)</asp:ListItem>
                       <asp:ListItem>504 Plan</asp:ListItem>
                       <asp:ListItem>Other</asp:ListItem>
                   </asp:CheckBoxList>

right now, when the other box is checked the panel displays (which is what I want).  But if I check on anything else it disappears.  The user should be able to check multiple boxes. But if Other is one of the checked boxes a panel should be visible. If Other is not checked or unchecked the the panel should disappear.  Here is the code that is supposed to run when other is checked.

Protected Sub cblEduAssistPlans_SelectedIndexChanged(sender As Object, e As EventArgs)
        If (cblEduAssistPlans.SelectedValue = "Other") Then
            pnlEduAssistPlans.Visible = True
        End If
    End Sub
SOLUTION
Avatar of louisfr
louisfr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cdlciddit
cdlciddit

ASKER

Thanks @louisfr and @David Johnson, CD, MVP.  I'm trying to get it to work now. I will report back shortly.
Hello @David Johnson, CD, MVP  I tried this. But when I do this it resets it to false every time I click on another page element b'c I have autopostback set to true in order for my radiobuttonlists to work.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Panel1.Visible = False
    End Sub

So instead I had to put all my panels in an if statement to check if it's visible each time the page does a postback
Hello @louisfr.  I also tried doing what you said.  At the beginning of the file in the page load I check to see if Other is a selected value but I don't this that works unless the user click Other first.  Here is that code:
If (cblEduAssistPlans.SelectedValue = "Other") Then
            pnlEduAssistPlans.Visible = True
        Else
            pnlEduAssistPlans.Visible = False
        End If

        If (cblEduAssistPlans.SelectedValue = "") Then
            pnlEduAssistPlans.Visible = False
        End If

Open in new window


Then I tried to do what you said and iterate through the list to get the values that have been selected.  Here is that code:
For Each li As ListItem In cblEduAssistPlans.Items
            If li.Selected = True Then Response.Write("cblEduAssistPlans2 " & li.Value & "<br>")
            If li.Value = "Other" Then pnlEduAssistPlans.Visible = True Else pnlEduAssistPlans.Visible = False

        Next
        Response.Write("<br>pnlEduAssistPlans: " & pnlEduAssistPlans.Visible)

Open in new window


Also. Each time the user makes a choice I want to check to see if they chose other so I can make the panel with the textbox appear.  Here is the code I used for that.
Protected Sub cblEduAssistPlans_SelectedIndexChanged(sender As Object, e As EventArgs)
        If (cblEduAssistPlans.SelectedValue = "Other") Then
            pnlEduAssistPlans.Visible = True
        End If
    End Sub

Open in new window


This is my checkbox list:
<asp:CheckBoxList ID="cblEduAssistPlans" runat="server" SelectedIndexChanged="cblEduAssistPlans_SelectedIndexChanged" AutoPostBack="true">
                       <asp:ListItem>Individualized education program (IEP)</asp:ListItem>
                       <asp:ListItem>Individualized Family Service Plan (IFSP)</asp:ListItem>
                       <asp:ListItem>504 Plan</asp:ListItem>
                       <asp:ListItem>Other</asp:ListItem>
                   </asp:CheckBoxList>

Open in new window


This is my panel:
<asp:Panel ID="pnlEduAssistPlans" runat="server">
           <table style="width:100%;">
               <tr>
                   <td>Enter assistance plan <asp:TextBox ID="txtEduAssistPlans" runat="server"></asp:TextBox></td>
               </tr>
           </table>
       </asp:Panel>

Open in new window


I don't know why this isn't working.  The visible value for pnlEduAssistPlans panel is always True.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hello @louisfr.  I'm sorry. No. I didn't try the panel code you provided. I wasn't sure how to implement it.   However I do understand the explanation you left above.  I will try this now and let you know if I'm able to get it working.  Thank you.
The one-line code checks if any item is selected and has the text "Other". It returns True if any item matches the condition, False otherwise.
Except for the Response.Write statements, it does the same thing as the loop.
Thanks @louisfr.  That worked!!!!!  That makes perfect sense now that you explained it.  Thanks a lot.
This is a perfect example of how to iterate through a asp checkbox list and check to see what values have been checked.