I was working with dynamically added TextBox controls in an unknown structure of recursively nested Repeaters the other day, and needed to loop through them after they were posted back to the server. Since I had no idea of where they were located, how many they were nor their IDs, I solved it in the simplest way that I could think of; by creating a recursive method looping through the entire page. Here is a generic version of that method created as a Control extension.
ControlExtensions.cs
public static class ControlExtensions { public static IEnumerable<T> GetAllControlsOfType<T>(this Control parent) where T : Control { var result = new List<T>(); foreach (Control control in parent.Controls) { if (control is T) { result.Add((T) control); } if (control.HasControls()) { result.AddRange(control.GetAllControlsOfType<T>()); } } return result; } }
There is really nothing complicated about this. The method checks to see if the control is of the generic type T that we are looking for (14), and adds it to a collection if it is. Then if the control has any child controls (18), the method is recursively used on all of these and the result is finally compiled in to one large IEnumerable. Since the System.Web.UI.Page class itself is an instance of System.Web.UI.Control it is possible to use this directly on the Page object.
Method usage
var allTextBoxesOnThePage = Page.GetAllControlsOfType<TextBox>(); var allHyperLinksInControl = randomControl.GetAllControlsOfType<HyperLink>();
Perfect! Saved me this tedious task. Great generic solution too.
You saved my day!!! Thanks!!!
Thanks for this man! Really needed a clear solution – appreciate it.
Thank you very much.
Hi,
Your code saved my day.
I was trying to find the dynamically added checkbox lists and to get the value of the checked items.
Now, with your code i got all the checkbox lists.
But, now i need to find Checkboxlists with ID starting with “FileList”.
ie, FileList1, FileList2, FileList3 etc.
how can i do that?
Can you please help me?
Regards,
Bhavana
Hi!
You can access the id value through the ID property of the control object; see for instance line 24 in this example. So perhaps changing line 14:
And perhaps also do a null check on the ID.
Cheers,
Mathias
Dude this is brilliant! Greetings from Russia. Nice solution!
Hi,
Thanks for your provided solution. I am currently trying to identify all controls that are present inside a webpage, but while using your solution code snippet, I am stuck. Visual studio is giving an error from the recursive call.
“result.AddRange(control.GetAllControlsOfType());”
Here the error thrown is : ” ‘System.Web.UI.Control’ does not contain a definition for ‘GetAllControlsOfType’ and no extension method for ‘GetAllControlsOfType’ accpeting a first argument of type ‘System.Web.UI.Control’ could be found (are you missing a using Directive or an assembly reference?)”
If this error is resolved I will be able to go ahead and scan a web page to get all the controls of the page. Any Help to resolve this error is appreciated.
Thanks
Aamir
Hi,
Did you change the code in any way? The extension method class needs to be static and you need the this in the argument list.
I suppose that you could omit it if you make a few alterations such as change the recursive call from control.GetAllControlsOfType<T>() to GetAllControlsOfType<T>(control)
Let me know how it works out for you.
Cheers!
Mathias