当调试我在DetailsView上遇到的问题时,在其中一个模板中查找控件时,我遇到了一些奇怪的事情。我已经实现了一个递归的FindControl扩展,它的作用是找到一个id与我要搜索的控件完全不同的控件。这个实现基本上是在父控件上调用Findcontrol,然后如果什么都没有找到,就在子控件上调用递归函数。
我开始深入研究带有反射器的asp.net代码,并发现如何实现checkboxs的FindControl方法(System.Web.UI.WebControls.CheckBoxList中的那个)。
protected override Control FindControl(string id, int pathOffset)
{
return this;
}现在这一切都说得通了,为什么我的FindControl找到了一个CheckBoxList,但是我看不出这个实现背后的原因,有人能告诉我吗?
发布于 2009-03-05 13:02:49
FindControl的这个实现覆盖了一个递归方法。
protected override Control FindControl(string id, int pathOffset)
{
return this;
}正在覆盖:
protected virtual Control FindControl(string id, int pathOffset)
{
string str;
this.EnsureChildControls();
if (!this.flags[0x80])
{
Control namingContainer = this.NamingContainer;
if (namingContainer != null)
{
return namingContainer.FindControl(id, pathOffset);
}我推测它会在不需要递归方法时使用,即已知当前控件就是您正在寻找的控件。
额外阅读MSDN
Control.FindControl在当前命名容器中搜索具有指定id和在pathOffset参数中指定的整数的服务器控件,这有助于搜索。不应重写此版本的FindControl方法。
id
The identifier for the control to be found.pathOffset
The number of controls up the page control hierarchy needed to reach a naming container. link
https://stackoverflow.com/questions/614063
复制相似问题