I need to go through a loop and check the proper radiobutton. I have multiple radio button named rb with a color such as “rbGreen, rbRed, rbYellow…”
Here is my code behind: (listColors is a list of strings)
Private Sub selectColor(color As String)
Dim i As Integer
For i = 0 To listColors.Count - 1
If listColors(i) = color Then
Dim rb As RadioButton = TryCast(Page.FindControl("rb" & color), RadioButton)
rb.Checked = True
End If
Next i
End Sub
While debugging, I got an error because rb is nothing…
My guess is that the
RadioButtons in question are not actually part of thePage, and are instead part of aUserControlor template based control (such as aRepeater).If so, then you need to modify your code to use the
FindControlof the control that holds theRadioButtons in question.If this is within a
UserControlthe easiest thing to do is something like…