色尼玛亚洲综合影院,亚洲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無效,轉載需保留來源!

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

主站蜘蛛池模板: 个旧市| 高平市| 军事| 巩留县| 兴海县| 阿克陶县| 河曲县| 中方县| 阿城市| 墨江| 保康县| 肃宁县| 天祝| 广丰县| 济南市| 汝阳县| 壤塘县| 台东县| 玉屏| 日土县| 洪湖市| 乌拉特中旗| 郓城县| 二连浩特市| 达日县| 蕉岭县| 那坡县| 靖宇县| 左权县| 十堰市| 双桥区| 龙南县| 威信县| 大方县| 比如县| 永和县| 大宁县| 阳原县| 保德县| 泰州市| 神木县|