Iterating through some controls on a webform

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
 
D

DrTeeth

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top