Web Design that taps into the haromny and vision of your dreams.

Using C# to Recursively Call FindControl in ASP.Net

By on in Coding

71 words, estimated reading time 1 minutes.

In ASP.Net I often need to use the recursively use FindControl to get an object reference to a control on the ASPX page. The only problem with this is that you need to know the control that it is contained with. This recursive method will search for a given control within a parent control and all it's child controls.

public static Control FindControlRecursive(Control container, string name)
    if ((container.ID != null) && (container.ID.Equals(name)))
        return container;
    foreach (Control ctrl in container.Controls)
        Control foundCtrl = FindControlRecursive(ctrl, name);
        if (foundCtrl != null)
            return foundCtrl;
    return null;


You need to pass in two parameters, a reference to a control to look in and the name of the control to find.


Control myControl = FindControlRecursive(PlaceHolder1, "myControl");

Last updated on: Wednesday 21st June 2017



Have a question or suggestion? Please leave a comment to start the discussion.



@roy : your function is nice but return type should be T instead of Control, otherwise using templates is pretty useless....

private T GetControl<T>(Control Container, string ControlID) where T : Control

Reply to Tigrou


Good post. This is the one we use.

private Control GetControl<T>(Control Container, string ControlID) where T : Control
T result = Container.FindControl(ControlID) as T;

if (result == null)
foreach (Control c in Container.Controls)
result = this.GetControl<T>(c, ControlID) as T;

if (result != null)

return result;

Reply to Roy


Leave a Reply

Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Your email address will not be published.