views:

57

answers:

2

I have a user control with 5 simple radiobuttons, I need ot loop through those in code-behind, but I am drawing a huge blank on how to do this. Can someone help please

+1  A: 
foreach (var ctl in this.Controls)
{
    if (ctl is RadioButton)
    {
       // stuff
    }
}

Note that this is not recursive. If your radiobuttons are further down in the control container heirarchy, you'll need to write a recursive method to find them. See my old answer here for an example of a recursive FindControl function.

womp
YOu cannot use "this.Controls" in a user control
mattgcon
Correction, you can you "this" but not outside any methods. I am trying to set a property as in (get; set;)
mattgcon
A: 

Just guessing here, but if you are trying to have a group of related radio buttons, you shouldn't be using individual radio button controls, but a RadioButtonList control. This will hold all of the radio buttons in a group and allow you to iterate over them.

Oded
I am using it this way specifically because of a request of the client. I prefer the radiobuttonlist but the require the labels to be on top of the buttons.
mattgcon
Use CSS to position the labels on top.
TheGeekYouNeed
Now I tried using CSS to position them but I couldn't get it to work.
mattgcon