这个也是很早的东西了,记得那时候.net的MVC还没盛行,基本是webForm的天下。现在虽然有用webform,但是的确已经很少继续使用了。重写现有的FindControl,用途就是根据控件ID查找指定的控件,一般情况下系统提供的方法就可以完成,但是那些被嵌套的控件用原来的方式无法查找,所以就重写了FindControl实现了被嵌套控件同样可以查找。这个也是在翻查一些记录的时候发现的,现在重新发出来做个记录吧!
public override Control FindControl(string id)
{
Control found = base.FindControl(id);
if (found == null)
{
found = this.Page.FindControl(id);
}
if (found == null)
{
found = FindControlExtend(id, this.Controls);
}
if (found == null)
{
found = FindControlExtend(id, this.Page.Controls);
}
return (found);
}
private Control FindControlExtend(string id, ControlCollection controls)
{
int i = 0;
Control Found = null;
for (; i < controls.Count; i++)
{
if (controls[i].ID == id)
{
Found = controls[i];
break;
}
if (controls[i].Controls.Count > 0)
{
Found = FindControlExtend(id, controls[i].Controls);
if (Found != null)
{
break;
}
}
}
return (Found);
}