Sometimes you want to work with all controls on an ASP.NET page by type rather than id. I finally got fed up with the lack of builtin support, and wrote the following method that returns all controls inside another control (or page) by type:
public List<T> FindControls<T>(Control parent) where <T> : Control
{
List<T> foundControls = new List<T>();
FindControls<T>(parent, foundControls);
return foundControls;
}
void FindControls<T>(Control parent, List<T> foundControls) where <T> : Control
{
foreach (Control c in parent.Controls)
{
if (c is <T>)
foundControls.Add((<T>)c);
else if (c.Controls.Count > 0)
FindControls<T>(parent, foundControls);
}
}