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="cblE duAssistPl ans_Select edIndexCha nged" AutoPostBack="true">
<asp:ListItem>Individualiz ed education program (IEP)</asp:ListItem>
<asp:ListItem>Individualiz ed Family Service Plan (IFSP)</asp:ListItem>
<asp:ListItem>504 Plan</asp:ListItem>
<asp:ListItem>Other</asp:L istItem>
</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_Selected IndexChang ed(sender As Object, e As EventArgs)
If (cblEduAssistPlans.Selecte dValue = "Other") Then
pnlEduAssistPlans.Visible = True
End If
End Sub
<asp:CheckBoxList ID="cblEduAssistPlans" runat="server" SelectedIndexChanged="cblE
<asp:ListItem>Individualiz
<asp:ListItem>Individualiz
<asp:ListItem>504 Plan</asp:ListItem>
<asp:ListItem>Other</asp:L
</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_Selected
If (cblEduAssistPlans.Selecte
pnlEduAssistPlans.Visible = True
End If
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
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:
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:
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.
This is my checkbox list:
This is my panel:
I don't know why this isn't working. The visible value for pnlEduAssistPlans panel is always True.
If (cblEduAssistPlans.SelectedValue = "Other") Then
pnlEduAssistPlans.Visible = True
Else
pnlEduAssistPlans.Visible = False
End If
If (cblEduAssistPlans.SelectedValue = "") Then
pnlEduAssistPlans.Visible = False
End If
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)
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
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>
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>
I don't know why this isn't working. The visible value for pnlEduAssistPlans panel is always True.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Except for the Response.Write statements, it does the same thing as the loop.
ASKER
Thanks @louisfr. That worked!!!!! That makes perfect sense now that you explained it. Thanks a lot.
ASKER
This is a perfect example of how to iterate through a asp checkbox list and check to see what values have been checked.
ASKER