C# Iterate through radio buttons in the panel - Foreach

3,156 views
Skip to first unread message

Sathesh Kumar

unread,
Jun 25, 2008, 3:24:05 AM6/25/08
to DotNetDe...@googlegroups.com
C#
 
Iterate through Radio Button controls  in panel using Foreach
 
Here is my code
 
 
foreach(RadioButton c in pnlradio.Controls)

{

if(c.Checked)

{

Response.Write(c.Text);

}

}

 
 
Error Message

System.InvalidCastException: Specified cast is not valid


********************************************************************************************************************************************************************

Regards
S..Sathesh Kumar
Mobile: 09951355483

Glenn

unread,
Jun 25, 2008, 2:11:26 PM6/25/08
to DotNetDe...@googlegroups.com
Your foreach loop is incorrect.  Doing it the way you specified is not going to get just the RadioButton controls from your container.  It is going to get EVERY control from your container.  In your case, it is getting another type of control and trying to typecast it to a RadioButton.  Hense the error you are getting.
 
...Glenn

 

Cerebrus

unread,
Jun 25, 2008, 2:25:03 PM6/25/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Glenn is quite right. You need to add another If condition around your
If. One that checks if the type of control is in fact a RadioButton,
because only they will have the Checked property.

foreach (Control c in pnlRadio.Controls)
{
if (typeof(c) is RadioButton)
{
if (c.Checked == true)
{
// OMG, they *checked* the radiobutton !!!
}
}
}

On Jun 25, 11:11 pm, Glenn <ke4ktza...@gmail.com> wrote:
> Your foreach loop is incorrect. Doing it the way you specified is not going
> to get just the RadioButton controls from your container. It is going to
> get EVERY control from your container. In your case, it is getting another
> type of control and trying to typecast it to a RadioButton. Hense the error
> you are getting.
>
> ...Glenn
>
> On 6/25/08, Sathesh Kumar <sathis...@gmail.com> wrote:
>
>
>
> > C#
>
> > *Iterate through Radio Button controls in panel using Foreach*
> > **
> > *Here is my code *
> > **
>
> > foreach(RadioButton c in pnlradio.Controls)
>
> > {
>
> > if(c.Checked)
>
> > {
>
> > Response.Write(c.Text);
>
> > }
>
> > }
>
> > *Error Message*
>
> > System.InvalidCastException: Specified cast is not valid**

Sathesh Kumar

unread,
Jun 26, 2008, 2:00:24 AM6/26/08
to DotNetDe...@googlegroups.com

foreach

(Control c in pnlradio.Controls)

{

if (c is RadioButton)

{

if(c.Check---     //Here I am not able to check whether its Checked ......

Response.Write(c.ID.ToString());

}

}

}

Cerebrus

unread,
Jun 26, 2008, 12:03:14 PM6/26/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Ahhhh, my bad ! That's what happens when I type the code in here... I
forget some important stuff such as the cast :

if (c is RadioButton)
{
RadioButton radio = c as RadioButton;
if (radio.Checked == true)
{
...
}
}

This is also typed in here, lolz !

On Jun 26, 11:00 am, "Sathesh Kumar" <sathis...@gmail.com> wrote:
> foreach (Control c in pnlradio.Controls)
>
> {
>
> if (c is RadioButton)
>
> {
>
> if(c.*Check--- //Here I am not able to check whether its Checked ......*

Glenn

unread,
Jun 26, 2008, 1:03:41 PM6/26/08
to DotNetDe...@googlegroups.com
Writing code while sleepy is a crime in most areas.  We'll let you off with a warning this time, but don't let it happen again! :)
 
...Glenn

 

Sathesh Kumar

unread,
Jun 27, 2008, 12:55:43 AM6/27/08
to DotNetDe...@googlegroups.com

Thank very much U  Cerebrus and Glenn for help me to know
 
 about Dynamic Controls....

I have dont with that....
Reply all
Reply to author
Forward
0 new messages