ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1603字,纯文字阅读大概需要3分钟。
内容图文
![ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效](/upload/InfoBanner/zyjiaocheng/381/80ebe4cb0e0e479b9af8938c8d3c349d.jpg)
代码如下:
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有效了。
内容总结
以上是互联网集市为您收集整理的ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效全部内容,希望文章能够帮你解决ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。