1

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;
        }
    }
}
AakashM
  • 62,551
  • 17
  • 151
  • 186
Lunetics
  • 95
  • 7
  • "It also takes a typeparam" suggests you are under a misapprehension. `SiteUserControlBase` and `SiteUserControlBase` are **completely different types** (in the same way that, say `Tuple` and `Tuple` are also completely different types) – AakashM Feb 06 '13 at 13:32

0 Answers0