ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效
网购男装,锦衣夜行最新章节,超鸡女生
代码如下:
panel spnbuttons = new panel();
button btn = new button();
btn.id = "btn1";
spnbuttons.controls.add(btn);
// 输出true,表示没有找到控件
response.write(spnbuttons.findcontrol(btn.id) == null);
而如果是下面的代码就可以了:
panel spnbuttons = new panel();
page.controls.add(spnbuttons);// 创建panel后把它加入page
button btn = new button();
btn.id = "btn1";
spnbuttons.controls.add(btn);
// 输出false,表示找到了控件
response.write(spnbuttons.findcontrol(btn.id) == null);
或者使用repeater也可以:
repeater spnbuttons = new repeater();
button btn = new button();
btn.id = "btn1";
spnbuttons.controls.add(btn);
// 输出false,表示找到了控件
response.write(spnbuttons.findcontrol(btn.id) == null);
查了一下panel是继承于webcontrol,而webcontrol的定义是:
public class webcontrol : control, iattributeaccessor
{}
repeater的定义是:
public class repeater : control, inamingcontainer
{}
难道是因为repeater实现了inamingcontainer的原因吗?
我又自定义了一个类,继承自panel,并实现了inamingcontainer,可以找到控件了:
public partial class webform1 : system.web.ui.page
{
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()
{
}
}
上,asp.net中,placeholder、panel等控件未实现inamingcontainer,导致findcontrol无效
如果把这些控件加入到实现了inamingcontainer的父控件中,或者用子类实现inamingcontainer,就可以使findcontrol有效了。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!!
点击进行留言回复
网友评论