473,414 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Iterating through some controls on a webform

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

Jul 9 '08 #1
2 1507
"DrTeeth" <no*@here.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Any help would be much appreciated. TIA
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
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 9 '08 #2
Ok thanks alot :)

"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
"DrTeeth" <no*@here.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>Any help would be much appreciated. TIA

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
--
Mark Rae
ASP.NET MVP
http://www.markrae.net
Jul 9 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: josema | last post by:
Hi to all... i have a class that inherit from WebControls.WebControl. This class generate a button an a textbox TextBox mytext=new TextBox() Button mybtn=new Button() this.controls.add(mytext)...
1
by: Charles A. Lackman | last post by:
Hello, I have created a User Control within Visual Studio and it contains a button that allows the user to querry a database. I dynamically add additional controls to the page based on the...
6
by: TomislaW | last post by:
How to find all user controls (ascx) loaded on a Page?
2
by: Dave | last post by:
I am converting an ASP Classic web site to an ASP.NET application and have some real basic formatting or design questions. (The classic ASP site has both code (forms) and content pages.) 1.When...
1
by: Chris | last post by:
This may be more of a Visual Studio question but those groups seem to be full of unrelated stuff so hopefully this might be the right place. I have a class (no associated aspx file) which handles...
4
by: Ali | last post by:
I used to clear my page's control in Visual Studio 2003 using code like this: Dim c As Control For Each c In Page.Controls(1).Controls If TypeOf c Is TextBox Then CType(c, TextBox).Text = Nothing...
2
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is...
4
by: WB | last post by:
Hi, How can I generate web controls such as textboxes and drop-menus on the fly? My web application allows users to fill out PDF forms online. There are many PDF forms, and my application...
8
by: MattB | last post by:
I have a asp.net 1.1/vb application that has a page with a bunch of dynamically added User Controls. When I add the controls, I set the UserControl.EnableViewState to true. For all my controls...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.