使用MasterPage后FindControl的使用
2008-05-09 23:42
211 查看
没有使用母版页前,查找控件可以这样使用
TextBox txt1=(TextBox)this.FindControl("TextBox1");
加入母版页后不能这样使用了
打开跟踪 Trace="true"
可以看到ID为TextBox1 的控件由于使用了母版页,控件前加入了MainContent
可以这样找到TextBox1 控件
TextBox txt1=this.Master.FindControl("MainContent").FindControl("TextBox1") as TextBox1 ;
即先用this.Master.FindControl("MainContent") 定位到TextBox1 的相对位置
加入母版页这里的“this”意义已经不是从前了,this的第一个对象找到的应该是MasterPage中的控件,而不是aspx中的控件。因为Aspx其实是把内容放在其中一个Content上的子控件上,所以aspx页面只能算作是this的一个子控件。
TextBox txt1=(TextBox)this.FindControl("TextBox1");
加入母版页后不能这样使用了
打开跟踪 Trace="true"
_ctl0:MainContent:TextBox1 | System.Web.UI.WebControls.TextBox | 109 | 0 | 0 |
_ctl0:MainContent:_ctl14 | System.Web.UI.ResourceBasedLiteralControl | 455 | 0 | 0 |
_ctl0:MainContent:_ctl3 | System.Web.UI.HtmlControls.HtmlTableCell | 78 | 0 | 0 |
_ctl0:MainContent:_ctl15 | System.Web.UI.LiteralControl | 83 | 0 | 0 |
_ctl0:MainContent:_ctl4 | System.Web.UI.HtmlControls.HtmlTableCell | 65 |
可以这样找到TextBox1 控件
TextBox txt1=this.Master.FindControl("MainContent").FindControl("TextBox1") as TextBox1 ;
即先用this.Master.FindControl("MainContent") 定位到TextBox1 的相对位置
加入母版页这里的“this”意义已经不是从前了,this的第一个对象找到的应该是MasterPage中的控件,而不是aspx中的控件。因为Aspx其实是把内容放在其中一个Content上的子控件上,所以aspx页面只能算作是this的一个子控件。
相关文章推荐
- 在使用了MasterPage后,FindControl()函数引起的问题
- 在使用MasterPage的Web窗体中FindControl
- 使用MasterPage后FindControl的使用
- 在使用了MasterPage后,FindControl()函数返回null的问题
- ASP.NET 2.0中动态混合使用MasterPage和主题
- 在母版页(MasterPage)中使用 jQuery 的一个小技巧
- 使用Masterpage母板页可以使站点具有一致的外观和统一的布局(图解)
- 在ASP.NET AJAX中,由于一个ASPX页面上只能有一个ScriptManager控件,所以在有母版页的情况下,如果需要在Master-Page和Content-Page中需要引入不同的脚本时,这就需要在Content-page中使用ScriptMa
- MasterPage事件使用
- 如何使用 MasterPage(注意母板页和子页面的执行顺序)
- 在MasterPage中使用javascript获取对象
- asp.net设置标题 兼MasterPage得使用
- Farseer.net轻量级开源框架 中级篇:BasePage、BaseController、BaseHandler、BaseMasterPage、BaseControls基类使用
- 在MasterPage中使用javascript获取对象
- 在MasterPage中使用javascript获取对象
- C# WebForm MasterPage Mobile.Master 此处不允许使用相对路径
- Masterpage中使用自定义控件
- ASP.NET2.0MasterPage使用技巧总结
- 解决使用MasterPage后,Page.FindControl方法找不到指定控件的问题
- 使用masterpage时,js访问服务端控件