在要求输入邮箱的文本域,请填写真实的邮件地址。非真实邮件地址,将收不到回复信息。

asp.net webform重写FindControl实现查找控件(包括被嵌套的控件)

DotNet 清风 834℃ 0评论

这个也是很早的东西了,记得那时候.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);
} 



转载请注明:清风亦平凡 » asp.net webform重写FindControl实现查找控件(包括被嵌套的控件)

喜欢 (2)or分享 (0)
支付宝扫码打赏 支付宝扫码打赏 微信打赏 微信打赏
头像
发表我的评论
取消评论

CAPTCHA Image
Reload Image
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址