Recursively Finding Controls

By Jonathan Wood on 3/22/2012
Language: C#
Technology: WebForms
Platform: Windows
License: CPOL
Views: 13,518
Web Development » ASP.NET » General » Recursively Finding Controls


All ASP.NET Web Form controls (and forms, which derive from Control) provide a FindControl() method. This method will search all the child controls of a given control to find the one that matches a given ID.

This can be a handy feature but it often comes up short because it is not recursive. That is, it searches a control's children, but not its children's children.

For example, if you try and find a control on a form, but that control exists inside a panel, then FindControl() will not be able to find it unless you specifically search the panel control. Since a web form page normally consists of a rich tree structure of controls and subcontrols, any general control search on a web form is likely to fail if the search is not recursive.

A Custom FindControl() Implementation

Fortunately, it is easy to write a version of FindControl() that is recursive. The FindControlRecursive() method in Listing 1 will search the child controls of a specified control, but it also searches the children's child controls (if needed).

It does this by enumerating the child controls and, for each control that is not the target control, FindControlRecursive() calls itself recursively to perform the same task on the child control. The result is that this method will search through the full depth of all child controls.

Listing 1: The FindControlRecursive() Control

/// <summary>
/// Recursively searches for a server control with the given ID.
/// </summary>
/// <param name="id">ID of control to find</param>
/// <returns>The matching control or null if no match was found</returns>
public static Control FindControlRecursive(this Control control, string id)
    foreach (Control ctl in control.Controls)
        if (ctl.ID == id)
            return ctl;

        Control child = FindControlRecursive(ctl, id);
        if (child != null)
            return child;
    return null;

Using the Code

To use the FindControlRecursive() method, you can call it just as you would call FindControl().

FindControlRecursive() is implemented as an extension method for the Control type. This means you can call the method as though it were a member method of the Control class, assuming the FindControlRecursive() method is visible from your code.

Listing 2 provides a very simple example for using this method. The code is assumed to exist in code behind a web form so that the this keyword refers to the form. This code returns the control with the id "btnAccept" even if the control exists within as a child of other controls. If the control is not found, the method returns null.

Listing 2: Calling the FindControlRecursive() Control

Control ctl = this.FindControlRecursive("btnAccept");


This was a very simple article but I thought this method was useful enough to share.

End-User License

Use of this article and any related source code or other files is governed by the terms and conditions of The Code Project Open License.

Author Information

Jonathan Wood

I'm a software/website developer working out of the greater Salt Lake City area in Utah. I've developed many websites including Black Belt Coder, Insider Articles, and others.

I hike each week with my dogs Suki and Sasha. You can see my hiking blog at Hiking Salt Lake.