最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2009年06月18日 21:22:00   作者:  
這2天在開發(fā)中發(fā)現(xiàn),如果在new的Panel中使用FindControl,會出現(xiàn)找不到控件的情況
代碼如下:
復(fù)制代碼 代碼如下:

Panel spnButtons = new Panel();
Button btn = new Button();
btn.ID = "btn1";
spnButtons.Controls.Add(btn);
// 輸出True,表示沒有找到控件
Response.Write(spnButtons.FindControl(btn.ID) == null);

而如果是下面的代碼就可以了:
復(fù)制代碼 代碼如下:

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也可以:
復(fù)制代碼 代碼如下:

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實現(xiàn)了INamingContainer的原因嗎?
我又自定義了一個類,繼承自Panel,并實現(xiàn)了INamingContainer,可以找到控件了:
復(fù)制代碼 代碼如下:

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等控件未實現(xiàn)INamingContainer,導(dǎo)致FindControl無效
如果把這些控件加入到實現(xiàn)了INamingContainer的父控件中,或者用子類實現(xiàn)INamingContainer,就可以使FindControl有效了。

相關(guān)文章

最新評論

祁阳县| 南开区| 静乐县| 博爱县| 屯门区| 平陆县| 桦甸市| 西城区| 嘉峪关市| 泰安市| 南汇区| 登封市| 青川县| 德州市| 循化| 青岛市| 西贡区| 天津市| 山阳县| 资中县| 双峰县| 大姚县| 东乌| 武川县| 通许县| 珠海市| 沅江市| 宁夏| 奉贤区| 惠安县| 民县| 尚志市| 泸定县| 梁平县| 台北市| 麻栗坡县| 建阳市| 镇原县| 遂川县| 张家港市| 津南区|