Superdopey’s Techblog

Recursive FindControl Method

| 0 comments

In tegenstelling tot wat je zou verwachten werkt de FindControl(string id) method niet recursief. Hier een implementatie die vanaf een parentcontrol alle childcontrols recursief doorzoekt naar het controlId.


public static T FindControlRecursive<t>(this Control parentControl, string id) where T : Control
{
T ctrl = default(T);

if ((parentControl is T) && (parentControl.ID == id))
return (T)parentControl;

foreach (Control c in parentControl.Controls)
{
ctrl = c.FindControlRecursive<t>(id);

if (ctrl != null)
break;
}
return ctrl;
}

Leave a Reply

Required fields are marked *.

*