ASP.NetPlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效
ASP 实现 控件 导致 无效 panel
2023-06-13 09:14:10 时间
代码如下:
复制代码代码如下:
PanelspnButtons=newPanel();
Buttonbtn=newButton();
btn.ID="btn1";
spnButtons.Controls.Add(btn);
//输出True,表示没有找到控件
Response.Write(spnButtons.FindControl(btn.ID)==null);
而如果是下面的代码就可以了:
PanelspnButtons=newPanel();
Page.Controls.Add(spnButtons);//创建Panel后把它加入Page
Buttonbtn=newButton();
btn.ID="btn1";
spnButtons.Controls.Add(btn);
//输出False,表示找到了控件
Response.Write(spnButtons.FindControl(btn.ID)==null);
或者使用Repeater也可以:
RepeaterspnButtons=newRepeater();
Buttonbtn=newButton();
btn.ID="btn1";
spnButtons.Controls.Add(btn);
//输出False,表示找到了控件
Response.Write(spnButtons.FindControl(btn.ID)==null);
查了一下Panel是继承于WebControl,而WebControl的定义是:
publicclassWebControl:Control,IAttributeAccessor
{}
Repeater的定义是:
publicclassRepeater:Control,INamingContainer
{}
难道是因为Repeater实现了INamingContainer的原因吗?
我又自定义了一个类,继承自Panel,并实现了INamingContainer,可以找到控件了:
publicpartialclassWebForm1:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
myPanelspnButtons=newmyPanel();
Buttonbtn=newButton();
btn.ID="btn1";
spnButtons.Controls.Add(btn);
Response.Write(spnButtons.FindControl(btn.ID)==null);
}
}
publicclassmyPanel:Panel,INamingContainer
{
publicmyPanel():base()
{
}
}
上,ASP.Net中,PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效
如果把这些控件加入到实现了INamingContainer的父控件中,或者用子类实现INamingContainer,就可以使FindControl有效了。
相关文章
- <asp: DropDownList>实现事件处理「建议收藏」
- 数据交互实现ASP页面与MySQL数据交互最佳方法(asp页面与mysql)
- 使用ASP链接MySQL实现快速查表(asp链接mysql查表)
- ASP连接MySQL实现优雅的数据查询(asp连接查询mysql)
- 基于ASP的MySQL数据库连接实现(asp连接数mysql)
- ASP实现与MySQL数据库的连接(asp连接mysql源码)
- ASP读取MySQL时间的方式(asp读取mysql时间)
- 库ASP实现MySQL数据库的轻松访问(asp读取mysql数据)
- ASP程序操作MySQL数据库实现数据库驱动开发(asp用mysql)
- ASP.NET动态加载用户控件的实现方法
- asp.netASPxTextBox等控件实现"回车模拟Tab"的常用代码整理
- asp.net中将数据库绑定到DataList控件的实现方法与实例代码
- 在ASP.NET中下载文件的实现代码
- asp.netGrid导出Excel实现程序代码
- asp.net静态方法弹出对话框实现思路
- ASP多条件查询功能实现代码(多关键词查询)
- ASP.Net中利用CSS实现多界面的两种方法