Loop through checkbox in webform to determine checked state

What do I have to do to make this code work?  I have a webform on it with 50 checkboxes and I want to build a text sting based on the state on the CheckBox.  When I debug this .Net only looops through the for...Next loop three times.

Dim ctl As Control
        For Each ctl In Page.Controls
            If ctl.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox") Then
                If CType(ctl, CheckBox).Checked = True Then
                    If selected = False Then
                        myRow.Item("Con_Code_Notes") = "Vehicle Options" + vbCrLf
                        myRow.Item("Con_Code_Notes") += CType(ctl, CheckBox).Text
                        selected = True
                    Else
                        myRow.Item("Con_Code_Notes") += ", " + CType(ctl, CheckBox).Text
                    End If
                End If
            End If
        Next ctl

I alos have tried this.

Dim ctl As Control
        For Each ctl In Me.Controls
            If ctl.GetType().ToString().Equals("System.Web.UI.WebControls.CheckBox") Then
                If CType(ctl, CheckBox).Checked = True Then
                    If selected = False Then
                        myRow.Item("Con_Code_Notes") = "Vehicle Options" + vbCrLf
                        myRow.Item("Con_Code_Notes") += CType(ctl, CheckBox).Text
                        selected = True
                    Else
                        myRow.Item("Con_Code_Notes") += ", " + CType(ctl, CheckBox).Text
                    End If
                End If
            End If
        Next ctl

and I tried this.

'Dim Ctl As Control
        'Dim box As CheckBox

        For Each Ctl In Me.Controls
            If TypeOf Ctl Is CheckBox Then
                box = DirectCast(Ctl, CheckBox)
                If box.Checked = True Then
                    If selected = False Then
                        myRow.Item("Con_Code_Notes") = "Vehicle Options" + vbCrLf
                        myRow.Item("Con_Code_Notes") += box.Text
                        selected = True
                    Else
                        myRow.Item("Con_Code_Notes") += ", " + box.Text
                    End If
                End If
            End If
        Next

Thanks in advance!
pamsautoAsked:
Who is Participating?
 
softplusConnect With a Mentor Commented:
Think recursive :) - you almost had it. This is just an example, you can skip the returned string part and just do your checkbox-stuff

   Function CheckCheckboxes(ByVal myControl As Control) As String
        Dim S As String
        Dim subControl As Control
        For Each subControl In myControl.Controls
            If subControl.ToString = "System.Web.UI.WebControls.CheckBox" Then
                Dim cb As CheckBox = _
                    DirectCast(myControl.FindControl(subControl.ClientID.ToString), CheckBox)
                S = S & "<br>Name=" & cb.ID.ToString & ", Checked=" & cb.Checked & ", text=" & cb.Text
                ' do whatever here :)
            End If
        Next
        For Each subControl In myControl.Controls
            S = S & CheckCheckboxes(subControl)
        Next
        CheckCheckboxes = S
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Response.Write(CheckCheckboxes(Page))
    End Sub

:)
0
 
b1xml2Commented:
Private Function GetCheckBoxNotes() As String
      Dim list As New ArrayList
      LoopForCheckBoxText(Me.Controls,list)
      Dim values As String() = DirectCast(list.ToArray(GetType(String)),String())
      Return ("Vehicle Options" & vbCrLf & String.Join(",",values))
End Sub

Private Sub LoopForCheckBoxText(ByVal controls As ControlCollection, ByVal list As ArrayList)
      For Each item As Control In controls
            If TypeOf ctl Is CheckBox AndAlso DirectCast(ctl,CheckBox).Checked Then
                  list.Add(DirectCast(ctl,CheckBox).Text)
            ElseIf ctl.Controls.Count > 0 Then
                  LoopForCheckBoxText(ctl.Controls,list)
            End If
      Next
End Sub

  myRow.Item("Con_Code_Notes") = GetCheckBoxNotes()





0
 
b1xml2Commented:
amendment
========
Private Sub LoopForCheckBoxText(ByVal controls As ControlCollection, ByVal list As ArrayList)
     For Each ctl As Control In controls
          If TypeOf ctl Is CheckBox AndAlso DirectCast(ctl,CheckBox).Checked Then
               list.Add(DirectCast(ctl,CheckBox).Text)
          ElseIf ctl.Controls.Count > 0 Then
               LoopForCheckBoxText(ctl.Controls,list)
          End If
     Next
End Sub
0
 
caball88Commented:
ok just to add to this solution somewhat you can place all your checkboxes into a panel. the you can loop through the control in the panel which will be less than all the controls on the form. my code is similar to that of the other two posters.

Dim cntl as Control
for each cntl in panel1.controls
      if TypeOf cntl is CheckBox then
             if CType(cntl, CheckBox).Checked then
                    sb.append(CType(cntl, CheckBox).Text)
                    sb.append(vbcrlf)
             end if
      end if
next

myRow.Item("Con_Code_Notes")  = sb.ToString()

just from my experience string concatentation is very slow performance wise and you do not want to loop though items building on a large string. 50 items is not bad but for a web application memory is going to be an issue because all users hit off the same machine. also wrapping up all the controls in a panel will reduce the iterations that the server has to process.
0
 
caball88Commented:
oops sorry the sb is a stringbuilder object, it is much faster than string concantentation and much better memory management.

Dim sb as New System.Test.StringBuilder
0
All Courses

From novice to tech pro — start learning today.