I´ve a base class for all user controls: SiteUserControlBase
. It also takes a typeparam: SiteUserControlBase<T>
. How do I best find all controls on the page that derives from SiteUserControlBase
regarding of the typeparam?
I use the extension method below to find all controls of type SiteUserControlBase
, but it will exclude all controls that also uses the type param.
public static IEnumerable<T> FindControlsOfType<T>(this ControlCollection Controls)
where T : class
{
T control;
foreach (Control ctrl in Controls)
{
if ((control = ctrl as T) != null)
{
yield return control;
}
foreach (T child in FindControlsOfType<T>(ctrl.Controls))
{
yield return child;
}
}
}