加入收藏 | 设为首页 | 会员中心 | 我要投稿 河北网 (https://www.hebeiwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindContro

发布时间:2020-05-10 20:13:52 所属栏目:编程 来源:站长网
导读: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();
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有用了。

(编辑:河北网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读