Everyone forgot a control should be searched recursively:
By name:
Control FindControl(Control control, string name) {
if (control.Name == name)
return control;
foreach(Control child in control.Controls) {
Control found = FindControl(control, name);
if (found != null)
return found;
}
return null;
}
The predicate is pretty bad: why by name? (Only because it was so in the Question.) Also, nobody will guarantee the name is unique in any scope.
Maybe, it's better to search by type.
The easiest way is generic. Here is how:
CONTROL_TYPE FindControl<CONTROL_TYPE>(Control control)
where CONTROL_TYPE : Control {
if (control is CONTROL_TYPE)
return (CONTROL_TYPE)control;
foreach (Control child in control.Controls) {
CONTROL_TYPE found =
FindControl<CONTROL_TYPE>(control);
if (found != null)
return found;
}
return null;
}
Generally whole design relying on search of control is pretty bad.
In some rare cases it can be useful. Anyway, it answers the question.
Good luck!