Pieter Brinkman Blog
C#: Get Parent Control with Generics

C#: Get Parent Control with Generics

1 min readpieterASP.NetC#ControlsC#Generics

I use the following method to return a parent control of a specific type. This method is recursive and uses generics.

private Control GetParentControl(Control control)  
{  
if (control.Parent.GetType() == typeof(T1))  
{  
return control.Parent;  
}  
else  
{  
return GetParentControl(control.Parent);  
}  
}