How do i detect selected values from a checkboxlist?
Posted on 2004-11-01
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_properties.manager_id WHERE property_id = " & property_id & " ORDER BY lname"
res = db.Query(qry)
cbl_send_report_to.DataSource = res
cbl_send_report_to.DataTextField = "name"
cbl_send_report_to.DataValueField = "id"
so cbl_send_report is the name of my checkboxlist.
On the default.aspx page i have:
RepeatColumns = "3"
RepeatDirection = "Vertical"
<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!")
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.