Recursive version of the FindControl(..) method

I don't know why there is none already implemented in the .NET framework. Anyway it wasn't too difficult to write one by myself:
/// <summary>
/// Recursive version of the FindControl method.
/// </summary>
/// <param name="parent">Parent to where to start the search for the control</param>
/// <param name="controlId">ID of the control to search for</param>
/// <returns></returns>
public static Control FindControlRecursive(Control parent, string controlId)
{
if (controlId == parent.ID)
return parent;

foreach (Control ctrl in parent.Controls)
{
Control tmp = FindControlRecursive(ctrl, controlId);
if (tmp != null)
return tmp;
}

return null;
}

I strongly recommend to check also this post.
Kindle

Comments

0

Your ad here?