the selected values in my checkboxlist are not adding correctly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cluce
    New Member
    • Sep 2007
    • 29

    the selected values in my checkboxlist are not adding correctly

    I am trying to add only my selected values but its only adding the first selected value in my list repeatedly to the amount of selected items. can someone help me with this?

    for example, lets say I check off 2 in my list. than I check off three other boxes are inline after 2's box in my list not before. no matter what value is in those boxes I still get 8. 2+2+2+2


    Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            lblAnswer.Text = ""
            ListBox1.Items.Clear()
            ''add up prices from list 
            Dim total As Decimal
            Dim i As Int32
    
            For i = 0 To CheckBoxList1.Items.Count - 1
                If CheckBoxList1.Items(i).Selected Then
                    total += Decimal.Parse(CDec(CheckBoxList1.SelectedValue.ToString))
                    ListBox1.Items.Add(CheckBoxList1.SelectedValue)
                End If
    
            Next
            lblAnswer.Text = total
    
        End Sub
    End Class
  • misza
    New Member
    • Feb 2008
    • 13

    #2
    Hi,

    what you're doing wrong is that you're using CheckBoxList1.S electedValue property for getting the value - it's showing list's first (of the lowest index) selected value instead of a "current iteration";

    pls take a look at this example (from msdn, slightly modified) of iterating through Checkboxlist's items

    Code:
    Dim i As Integer
    For i = 0 To checkboxlist1.Items.Count - 1
        If checkboxlist1.Items(i).Selected Then
           ' the value you're looking for is in checkboxlist1.Items(i).Text
        End If
    Next
    hope this helps,
    M.

    Comment

    Working...