欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

Panel spnButtons = new Panel();
Button btn = new Button();
btn.ID = "btn1";
spnButtons.Controls.Add(btn);
// 輸出True,表示沒(méi)有找到控件
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
{}
難道是因?yàn)镽epeater實(shí)現(xiàn)了INamingContainer的原因嗎?
我又自定義了一個(gè)類,繼承自Panel,并實(shí)現(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等控件未實(shí)現(xiàn)INamingContainer,導(dǎo)致FindControl無(wú)效
如果把這些控件加入到實(shí)現(xiàn)了INamingContainer的父控件中,或者用子類實(shí)現(xiàn)INamingContainer,就可以使FindControl有效了。

相關(guān)文章

最新評(píng)論