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)找不到控件的情況
代碼如下:
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);
而如果是下面的代碼就可以了:
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也可以:
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,可以找到控件了:
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有效了。
復(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有效了。
您可能感興趣的文章:
- ASP.NET中CheckBoxList復(fù)選框列表控件詳細(xì)使用方法
- ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
- ASP.NET中FileUpload文件上傳控件應(yīng)用實(shí)例
- ASP.NET中HiddenField隱藏域控件的使用方法
- ASP.NET中Image控件使用詳解
- ASP.NET中ImageButton圖片按鈕控件的使用
- ASP.NET 中 Button、LinkButton和ImageButton 三種控件的使用詳解
- ASP.NET 中ImageMap控件的用法
- ASP.NET中Label控件用法詳解
- ASP.NET中 ListBox列表框控件的使用方法
- ASP.NET中Literal控件的使用方法
- ASP.NET中 PlaceHolder 控件的使用方法
相關(guān)文章
asp.net(c#)做一個(gè)網(wǎng)頁(yè)數(shù)據(jù)采集工具
最近做一個(gè)網(wǎng)站,該網(wǎng)站需要添加4000多 產(chǎn)品信息,如果用人工方法去別的網(wǎng)站copy那至少要花費(fèi)半月時(shí)間才能完成,所以我個(gè)辦法使用c#作出來(lái)了一個(gè)網(wǎng)頁(yè)數(shù)據(jù)采集軟件.2009-12-12在.NET?Core中使用CSRedis的詳細(xì)過(guò)程
這篇文章主要介紹了在.NET?Core中使用CSRedis的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06asp.net jQuery Ajax用戶登錄功能的實(shí)現(xiàn)
前幾天把jbox源碼修改成仿QQ空間模擬窗口后發(fā)現(xiàn)有很多人在關(guān)注。今天就貼一下我利用該模擬窗口實(shí)現(xiàn)的用戶登錄功能的代碼。2009-11-11asp.net C#實(shí)現(xiàn)下載文件的六種方法實(shí)例
asp.net C#實(shí)現(xiàn)下載文件的六種方法實(shí)例,需要的朋友可以參考一下2013-04-04asp.net中CSharpThinking擴(kuò)展方法分析
這篇文章主要介紹了asp.net中CSharpThinking擴(kuò)展方法,實(shí)例講述了擴(kuò)展方法的特征及應(yīng)用,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11Asp.net 2.0 無(wú)刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
簡(jiǎn)單三步實(shí)現(xiàn)圖片無(wú)刷新上傳:注意是上傳,至于上傳時(shí)的驗(yàn)證,比如圖片的尺寸,大小,格式。自行解決。如果我搞定了,也會(huì)貼上來(lái)的。2013-06-06ASP.NET MVC4 利用uploadify.js多文件上傳
本文主要介紹了ASP.NET MVC4利用uploadify.js實(shí)現(xiàn)多文件上傳的方法代碼。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03