FPIT
asked on
How do i detect selected values from a checkboxlist?
I am having trouble detecting which items are selected in my checkboxlist. On the page_load event i populate the checkboxlist with information from the database and i bind it. When the application runs i want the user to be able to select multi options from the checkboxlist and click "Submit" where a script runs to detect what the user selected. Sounds very simple to me, but i have cursed many many times trying to get it to work. Here's what i have:
CODE BEHIND PAGE
======================
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'I have other code here too
qry = "SELECT id, (' ' + lname + ', ' + fname) AS name FROM managers LEFT OUTER JOIN managers_to_properties ON
managers.id=managers_to_pr operties.m anager_id WHERE property_id = " & property_id & " ORDER BY lname"
res = db.Query(qry)
cbl_send_report_to.DataSou rce = res
cbl_send_report_to.DataTex tField = "name"
cbl_send_report_to.DataVal ueField = "id"
cbl_send_report_to.DataBin d()
End Sub
so cbl_send_report is the name of my checkboxlist.
On the default.aspx page i have:
<asp:CheckBoxList
id="cbl_send_report_to"
RepeatColumns = "3"
RepeatDirection = "Vertical"
CssClass="cbl"
Runat="server" />
<asp:Button ID="btn_enter" Text="Enter System" OnClick="btn_enter_Click" CssClass="btn" Runat="server" BorderStyle="Outset" />
So when the user clicks the "Enter System" button, it triggers the btn_enter_Click sub....which goes as follows:
Sub btn_enter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_enter.Click
Dim selItem As ListItem
For Each selItem In cbl_send_report_to.Items
If selItem.Selected Then
Response.Write("this should definitly work!")
End If
Next
End Sub
That's about it. I have tried a For Next loop as well by finding out how many checkboxes there are and cycling through each one looking for selected ones. At any rate, netiher have worked. Each time i try it by printing out selItem.selected, it says False. So, i don't know what else to do here. By the way, the code executes with no errors, it just doesn't do what i want it to.
Any ideas?
CODE BEHIND PAGE
======================
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'I have other code here too
qry = "SELECT id, (' ' + lname + ', ' + fname) AS name FROM managers LEFT OUTER JOIN managers_to_properties ON
managers.id=managers_to_pr
res = db.Query(qry)
cbl_send_report_to.DataSou
cbl_send_report_to.DataTex
cbl_send_report_to.DataVal
cbl_send_report_to.DataBin
End Sub
so cbl_send_report is the name of my checkboxlist.
On the default.aspx page i have:
<asp:CheckBoxList
id="cbl_send_report_to"
RepeatColumns = "3"
RepeatDirection = "Vertical"
CssClass="cbl"
Runat="server" />
<asp:Button ID="btn_enter" Text="Enter System" OnClick="btn_enter_Click" CssClass="btn" Runat="server" BorderStyle="Outset" />
So when the user clicks the "Enter System" button, it triggers the btn_enter_Click sub....which goes as follows:
Sub btn_enter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_enter.Click
Dim selItem As ListItem
For Each selItem In cbl_send_report_to.Items
If selItem.Selected Then
Response.Write("this should definitly work!")
End If
Next
End Sub
That's about it. I have tried a For Next loop as well by finding out how many checkboxes there are and cycling through each one looking for selected ones. At any rate, netiher have worked. Each time i try it by printing out selItem.selected, it says False. So, i don't know what else to do here. By the way, the code executes with no errors, it just doesn't do what i want it to.
Any ideas?
You have to use the CheckedItems collection
Try:
Dim selItem As ListViewItem
For Each selItem In cbl_send_report_to.Checked Items
If selItem.Selected() Then
Response.Write("this should definitly work!")
End If
Next
Try:
Dim selItem As ListViewItem
For Each selItem In cbl_send_report_to.Checked
If selItem.Selected() Then
Response.Write("this should definitly work!")
End If
Next
Oops... let's try that again
Dim i As Integer
For i = 0 To cbl_send_report_to.Checked Items.Coun t - 1
With cbl_send_report_to.Checked Items
Response.Write("This is the text of the selected item: " & .Item(i))
End With
Next
Dim i As Integer
For i = 0 To cbl_send_report_to.Checked
With cbl_send_report_to.Checked
Response.Write("This is the text of the selected item: " & .Item(i))
End With
Next
ASKER
Thanks, i tried that, but i don't get checkedItems as an option in the intellisense and it underlines each line that has the CheckItems.Count in it.
I tried using selectedItems, or using a For loop to loop through like this:
Dim x As Integer
Dim count As Integer
count = cbl_send_report_to.items.c ount - 1
For x = 0 To count
If cbl_send_report_to.Items(x ).Selected Then
Response.Write("Selected item: " & cbl_send_report_to.items(x ).Text)
End If
Next
That don't work either. Any other ideas? I've never seen CheckedItems as an option before. Are you sure that's used for checkboxlists? Thanks again.
I tried using selectedItems, or using a For loop to loop through like this:
Dim x As Integer
Dim count As Integer
count = cbl_send_report_to.items.c
For x = 0 To count
If cbl_send_report_to.Items(x
Response.Write("Selected item: " & cbl_send_report_to.items(x
End If
Next
That don't work either. Any other ideas? I've never seen CheckedItems as an option before. Are you sure that's used for checkboxlists? Thanks again.
ASKER
I must have something declared wrong or something....everything i find on the net says that the For loop that i entered above should work! But it doesn't. I'm rotted!
Here's how the checkboxlist is declared in my code behind page:
Protected WithEvents cbl_send_report_to As System.Web.UI.WebControls. CheckBoxLi st
That should be correct....
I'm lost here.
Here's how the checkboxlist is declared in my code behind page:
Protected WithEvents cbl_send_report_to As System.Web.UI.WebControls.
That should be correct....
I'm lost here.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
HAHA.....THANKS SOOOOOOOOOOOOO MUCH.
I can't believe i was soooo stupid to miss that! I knew i had the other parts done right because i did it before, i just forgot the Not IsPostBack statement!! Arghhhhh!!
Thanks again, it works now...good job!
I can't believe i was soooo stupid to miss that! I knew i had the other parts done right because i did it before, i just forgot the Not IsPostBack statement!! Arghhhhh!!
Thanks again, it works now...good job!
Good catch, Glom.
And Oops (to me) -- FPIT, that code snipet for a System.Windows.Forms.Check edListBox, not your System.Web.UI.WebControls. CheckBoxLi st. Sorry for the confusion.
And Oops (to me) -- FPIT, that code snipet for a System.Windows.Forms.Check
ASKER
Cool...thanks for you help too LandyJ
ASKER
Thanks, any help is appreciated!