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

首頁 > 編程 > .NET > 正文

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

2020-01-18 01:14:20
字體:
來源:轉載
供稿:網友
代碼如下:
復制代碼 代碼如下:

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有效了。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 仙桃市| 繁峙县| 潼关县| 义乌市| 明星| 临沧市| 乌兰浩特市| 鄂尔多斯市| 漳浦县| 金门县| 富川| 辽阳市| 余姚市| 东平县| 东乡族自治县| 邛崃市| 分宜县| 津南区| 城固县| 类乌齐县| 壶关县| 定安县| 仪征市| 洪洞县| 宜城市| 玉田县| 东平县| 苏尼特右旗| 沾益县| 宜阳县| 泰安市| 资兴市| 蓝山县| 宁城县| 大名县| 抚顺市| 张家川| 绍兴市| 嫩江县| 普宁市| 独山县|