ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

FindControlR

Recursive find control method used for finding controls within templates.

Source

public static Control FindControlR(this Control root, string id)
{
    System.Web.UI.Control controlFound; 
    if (root != null) 
    { 
        controlFound = root.FindControl(id);
        if (controlFound != null)
        {
            return controlFound;
        }
        foreach (Control c in root.Controls) 
        {
            controlFound = c.FindControlR(id);
            if (controlFound != null)
            {
                return controlFound;
            }
        } 
    } 
    return null;
}

Example

Label theLabel = form1.FindControlR("ControlToFind") as Label;
if (theLabel != null)
{
    theLabel.Text = "Found it!";
}

Author: Brendan Enrick

Submitted on: 3 jul. 2008

Language: C#

Type: System.Web.UI.Control

Views: 5287