国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.Net PlaceHolder、Panel等控件未實(shí)現(xiàn)INamingContainer,導(dǎo)致FindContro

2024-07-10 12:38:46
字體:
供稿:網(wǎng)友
代碼如下:
代碼如下:
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);// 創(chuàng)建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
{}
難道是因?yàn)镽epeater實(shí)現(xiàn)了INamingContainer的原因嗎?
我又自定義了一個類,繼承自Panel,并實(shí)現(xiàn)了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等控件未實(shí)現(xiàn)INamingContainer,導(dǎo)致FindControl無效
如果把這些控件加入到實(shí)現(xiàn)了INamingContainer的父控件中,或者用子類實(shí)現(xiàn)INamingContainer,就可以使FindControl有效了。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿瓦提县| 定南县| 阳东县| 延庆县| 崇明县| 上林县| 鄂温| 安顺市| 明水县| 元氏县| 云浮市| 漳州市| 罗城| 札达县| 尚志市| 晋江市| 卓尼县| 静安区| 清河县| 金平| 广东省| 黎平县| 四子王旗| 东辽县| 湘乡市| 杭州市| 抚州市| 邓州市| 红河县| 商都县| 兰考县| 宣汉县| 招远市| 金平| 海阳市| 黄浦区| 虞城县| 江华| 木兰县| 陵水| 青州市|