Zarichney
Zarichney

Reputation: 165

How to modify property of radiobutton without using its id tag

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

Answers (1)

freefaller
freefaller

Reputation: 19953

My guess is that the RadioButtons 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 RadioButtons in question.

If this is within a UserControl the easiest thing to do is something like...

Me.FindControl("rb" & color)

Upvotes: 1

Related Questions