色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

ASP.Net PlaceHolder、Panel等控件未實現INamingContainer,導致FindControl無效

代碼如下:
復制代碼 代碼如下:
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有效了。

AspNet技術ASP.Net PlaceHolder、Panel等控件未實現INamingContainer,導致FindControl無效,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 綦江县| 德令哈市| 石嘴山市| 布尔津县| 井研县| 沙田区| 金寨县| 靖州| 西乡县| 沅江市| 阜阳市| 萨迦县| 酉阳| 林口县| 遵义县| 霞浦县| 垫江县| 佛学| 阿拉善盟| 确山县| 广州市| 屯昌县| 松滋市| 呼图壁县| 元朗区| 周至县| 黄山市| 临湘市| 江津市| 城步| 临邑县| 桐乡市| 保山市| 石城县| 黔东| SHOW| 沙田区| 延边| 昆明市| 东丽区| 定州市|