详细问题,我写在这里了:http://blog.csdn.net/youbl/archive/2009/06/04/4241465.aspx大意就是new的Panel,如果没有加入到Page.Controls中,它的FindControl就失效,
public class myPanel : Panel, INamingContainer 这种Panel的子类,FindControl也无效而PlaceHolder也是一样,如果没有加入到Page.Controls中,它的FindControl就失效,
可是子控件:
public class myPlaceHolder : PlaceHolder, INamingContainer ,FindControl就有效请大侠指点原因。注:不散分,只给回答有帮助的分
public class myPanel : Panel, INamingContainer 这种Panel的子类,FindControl也无效而PlaceHolder也是一样,如果没有加入到Page.Controls中,它的FindControl就失效,
可是子控件:
public class myPlaceHolder : PlaceHolder, INamingContainer ,FindControl就有效请大侠指点原因。注:不散分,只给回答有帮助的分
PlaceHolder spnButtons = new PlaceHolder();
spnButtons.ID = "snp1";
Button btn = new Button();
btn.ID = "btn1";
spnButtons.Controls.Add(btn);
// 这里还是输出True
Response.Write(spnButtons.FindControl(btn.ID) == null); Response.End();3楼的兄弟,
WebControl也是继承自Control的
{
MyPanel panel = new MyPanel();
Label label=new Label();
label.ID="pp";
label.Text = "ok";
panel.Controls.Add(label); Label lb=(Label)panel.FindControl("pp");
string str = lb.Text; }
MyPanel就是继承自System.Web.UI.HtmlControls.Panel 并且实现了INamingContainer。
对 但是有重写
另外 我刚才看了下你的blog文章 你的FindControl骑驴找驴了
同样也会找不到PANLE里的控件
再去研究研究REPEATER
{
protected void Page_Load(object sender, EventArgs e)
{
myPanel spnButtons = new myPanel(); Button btn = new Button();
btn.ID = "btn1";
spnButtons.Controls.Add(btn); Response.Write(spnButtons.FindControl(btn.ID) == null);
}
} public class myPanel : Panel, INamingContainer
{
public myPanel()
: base()
{
}
}
谢谢这位兄弟
只要控件实现INamingContainer就可以使用FindControl方法了结帖了
{
myPanel spnButtons = new myPanel(); Button btn = new Button();
btn.ID = "btn1";
btn.Text = "这是一个按钮"; spnButtons.Controls.Add(btn); Response.Write(spnButtons.FindControl(btn.ID) == null); Response.Write(((Button)spnButtons.FindControl(btn.ID)).Text);
} public class myPanel : Panel, INamingContainer
{
public myPanel()
: base()
{
}
}
发现Repeater和GridView等是在“数据”控件组。
而Panel是在“标准”控件组。
其实,这也不能说明啥,但是,我在一个页面中放了一个Panel,在Panel中放了一个TextBox。
写了段代码: // ob为null
Control ob = this.Repeater1.FindControl("TextBox1");
// ob1发现控件
Control ob1 = this.FindControl("TextBox1");
这么说明,TextBox“注册”在的是Page中,而不是Panel中。
为啥为这样,我也不知道。
个人想法:
Panel只是个包容其他控件的一个形式控件,它没有像GridView要写
<Columns>
<asp:TemplateField>
</asp:TemplateField>
</Columns>
这些,就是个说明。
其实它注册在的是Page.
所以你在这个控件中Find其他控件为null。
FrameWork源码: 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);
}
return null;
}
if (this.HasControls() && (this._occasionalFields.NamedControls == null))
{
this.EnsureNamedControlsTable();
}
if ((this._occasionalFields == null) || (this._occasionalFields.NamedControls == null))
{
return null;
}
char[] anyOf = new char[] { '$', ':' };
int num = id.IndexOfAny(anyOf, pathOffset);
if (num == -1)
{
str = id.Substring(pathOffset);
return (this._occasionalFields.NamedControls[str] as Control);
}
str = id.Substring(pathOffset, num - pathOffset);
Control control2 = this._occasionalFields.NamedControls[str] as Control;
if (control2 == null)
{
return null;
}
return control2.FindControl(id, num + 1);
}
看红的就构了.如果进入到红的地方.就说明控件没有实现INamingContainer接口.也就是INamingContainer==null所以返回NULL
{
get
{
if ((this._namingContainer == null) && (this.Parent != null))
{
if (this.Parent.flags[0x80])
{
this._namingContainer = this.Parent;
}
else
{
this._namingContainer = this.Parent.NamingContainer;
}
}
return this._namingContainer;
}
}漏了一点.这一点解释了为什么加到FORM里面后,就可以找到了.这是一个递归的过程.就是找父级的namingContainer如果找到了,自然也能找到这一层namingContainer所对应的CONTROL