I have been pondering how I can get all controls on a page and then perform a task on them in this related question:
How to Search Through a C# DropDownList Programmatically
I need code that can scan the page, get all DropDownList Controls and return them in a list.
I'm currently having to edit each individual control, I would rather be able to dynamically loop over each control to preform my task.
Here is a recursive version that returns a control collection of the requested type instead of utilizing another argument:
Insert into your class (static optional).
Looping through controls on a page isn't hard - you just have to look within each control for more controls.
You could do something like
You can use recursive logic to get all of the controls, like this:
This works if you use the form components from system.web.ui however this does not work when you use them from system.web.mvc apparently so i came up with the following work around.
This works for me however i found out that radio button if not selected is null so doesnt return anything which is ok i dont have to write anything to the database if it is null