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
2010-09-30 20:27:07
YOu cannot use "this.Controls" in a user control
mattgcon
2010-09-30 23:31:32
Correction, you can you "this" but not outside any methods. I am trying to set a property as in (get; set;)
mattgcon
2010-09-30 23:55:21
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
2010-09-30 20:27:53
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
2010-09-30 23:31:02