简体   繁体   中英

VB.net For Loop to capture data from multiple controls (CheckBoxList Items)

I have two CheckBoxList controls ( chkListVideoMedia and chkListAudioMedia ) on my page that I want to capture information from and insert the records into the database. I have it working for one of the controls, I just need help modifying my code below to include the second CBL

Dim values As New ArrayList()
For counter As Integer = 0 To chkListVideoMedia.Items.Count - 1
    If chkListVideoMedia.Items(counter).Selected Then
        MyTextBox.Text = chkListVideoMedia.Items(counter).Value
        values.Add(newId)
    End If
Next
If values.Count > 0 Then
  For item As Integer = 0 To values.Count - 1
    If item = 0 Then
      MyMedia1.Text = values(item).ToString
    End If
    If item = 1 Then
      MyMedia2.Text = values(item).ToString
    End If
    If item = 2 Then
      MyMedia3.Text = values(item).ToString
    End If
    If item = 3 Then
      MyMedia4.Text = values(item).ToString
    End If
  Next
End If

Thanks, James

You can find out which Collection has the most Items, then check to make sure that count is not greater than the maximum Items in each collection. Something like this.

Dim values As New ArrayList()
Dim counter As Integer
If chkListVideoMedia.Items.Count > chkListAudioMedia.Items.Count Then
    counter = chkListVideoMedia.Items.Count - 1
Else
    counter = chkListAudioMedia.Items.Count - 1
End If
For x = 0 To counter
    If Not (counter > chkListVideoMedia.Items.Count - 1) Then
        'Do your work here
    End If
    If Not (counter > chkListAudioMedia.Items.Count - 1) Then
        'Do your work here
    End If
Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM