Reputation: 165
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...
Upvotes: 1
Views: 67
Reputation: 19953
My guess is that the RadioButton
s in question are not actually part of the Page
, and are instead part of a UserControl
or template based control (such as a Repeater
).
If so, then you need to modify your code to use the FindControl
of the control that holds the RadioButton
s in question.
If this is within a UserControl
the easiest thing to do is something like...
Me.FindControl("rb" & color)
Upvotes: 1