Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do i detect selected values from a checkboxlist?

Posted on 2004-11-01
9
Medium Priority
?
221 Views
Last Modified: 2010-05-18
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"
            cbl_send_report_to.DataBind()

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?
0
Comment
Question by:FPIT
[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
  • 5
  • 3
9 Comments
 

Author Comment

by:FPIT
ID: 12466572
Also to give you a little more info...i just tried this by removing the databind section in the page load sub and i hard coded in some values on the html or default.aspx page to try it that way and it worked!  So the problem must have something to do with using databind??  At any rate, i HAVE to generate the checkboxlist from a database as it changes depending on who's logged in.   Just thought that may help too!  

Thanks, any help is appreciated!
0
 
LVL 7

Expert Comment

by:LandyJ
ID: 12467850
You have to use the CheckedItems collection
Try:

 Dim selItem As ListViewItem
  For Each selItem In cbl_send_report_to.CheckedItems
        If selItem.Selected() Then
              Response.Write("this should definitly work!")
        End If
  Next
0
 
LVL 7

Expert Comment

by:LandyJ
ID: 12468856
Oops... let's try that again

    Dim i As Integer
    For i = 0 To cbl_send_report_to.CheckedItems.Count - 1
      With cbl_send_report_to.CheckedItems
        Response.Write("This is the text of the selected item: " &  .Item(i))
      End With
    Next

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:FPIT
ID: 12471853
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.count - 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.
0
 

Author Comment

by:FPIT
ID: 12472724
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.CheckBoxList

That should be correct....  


I'm lost here.  
0
 
LVL 4

Accepted Solution

by:
Glom earned 500 total points
ID: 12474611
Hi,

I think you are not searching in the right direction,
I guess the problem comes from your page_load handling :
- Before your btn_enter_Click method is called, the Form_Load method is automatically launched
- That makes your cbl_send_report_to to be re-initialized each time before calling the btn_enter_Click method

Try to put this in your form_load event handler

If Not IsPostBack Then
            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"
            cbl_send_report_to.DataBind()
End If

Bye
0
 

Author Comment

by:FPIT
ID: 12474707
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!  
0
 
LVL 7

Expert Comment

by:LandyJ
ID: 12475170
Good catch, Glom.

And Oops (to me) -- FPIT, that code snipet for a System.Windows.Forms.CheckedListBox, not your System.Web.UI.WebControls.CheckBoxList.  Sorry for the confusion.
0
 

Author Comment

by:FPIT
ID: 12475208
Cool...thanks for you help too LandyJ  
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

610 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