public static class ControlExtensions
{
public static T FindControl<T>(this Control currentControl, string id) where T : Control
{
if (id.Equals(currentControl.ID, StringComparison.OrdinalIgnoreCase))
{
// return control calling this method
return currentControl as T;
}
// initialize potential "found" control to null
T potentialFoundControl = null;
foreach (Control childControl in currentControl.Controls)
{
// recursive call to each child control
potentialFoundControl = ControlExtensions.FindControl<T>(childControl, id);
// if result is not null, match has been found, break the loop
if (potentialFoundControl != null) { break; }
}
return potentialFoundControl;
}
}