Iterating through some controls on a webform

  • Thread starter Thread starter DrTeeth
  • Start date Start date
D

DrTeeth

I new to ASP.NET and got slightly confused on a probably simple little
issue.

I am try to iterate through some ASP radio buttons to see which of them is
checked.

But I get the error :

Unable to cast object of type 'System.Web.UI.LiteralControl' to type
'System.Web.UI.WebControls.RadioButton'.

The error occurs in the line : For Each rdbChecked In Me.Controls

Any help would be much appreciated. TIA

Protected Sub cmdSearch_Click(ByVal sender As Object, ByVal e As
System.EventArgs)

Dim rdbChecked As RadioButton
Dim sURL As String = ""
For Each rdbChecked In Me.Controls
If rdbChecked.Checked Then
sURL = GetSearchString(rdbChecked.ID) & txtSearch.Text
End If
Next

Response.Redirect(sURL)

End Sub
 
Ok thanks alot :)

Mark Rae said:
The error is caused because not every Control in the form is a
RadioButton.

So you would need something like:

Dim objControl As Control
For Each objControl In Me.Controls
If objControl.GetType().Name = "RadioButton" Then
If rdbChecked.Checked Then
' whatever
End If
End If
Next
 
Back
Top