Juri Strumpflohner
Juri Strumpflohner Juri is a full stack developer and tech lead with a special passion for the web and frontend development. He creates online videos for Egghead.io, writes articles on his blog and for tech magazines, speaks at conferences and holds training workshops.

Recursive version of the FindControl(..) method

1 min read

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.
Questions? Thoughts? Hit me up on Twitter
comments powered by Disqus