加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 嵌套的Masterpages和.FindControl

发布时间:2020-12-15 04:10:10 所属栏目:百科 来源:网络整理
导读:在一个站点上,我只使用单级主页,在使用该主页的页面中,我可以执行此操作.Master.FindControl(“controlName”)来访问该控件.工作良好. 但是,在具有两个母版页级别的站点上使用相同的代码.以MainMaster为主的MainMaster和SpecificMaster. 因此,在使用Specific
在一个站点上,我只使用单级主页,在使用该主页的页面中,我可以执行此操作.Master.FindControl(“controlName”)来访问该控件.工作良好.

但是,在具有两个母版页级别的站点上使用相同的代码.以MainMaster为主的MainMaster和SpecificMaster.

因此,在使用SpecificMaster的页面上,FindControl为对象返回null.我看到的唯一区别是主页的嵌套.

当我设置断点并查看page.Master时,它显示了SpecificMaster,并且SpecificMaster正确显示MainMaster作为其主节点,但FindControl仍然失败.

当我在IE中查看源代码时,控件被正确命名,没有.NET正在进行中.

这有什么想法?

TIA!

解决方法

当您嵌套母版页时,您将获得一个额外的容器“内容”,您需要查看.

因此,如果您尝试从给定子页面使用FindControl,通常的方法是:

Label myLabel = (Label)this.Master.FindControl("myLabel");
myLabel.Text = "Success!";

由于我们有一个嵌套的母版页,在子母版中有“myLabel”,因此该控件将包含在内容控件中.

因此,这会将代码更改为:

ContentPlaceHolder ph = (ContentPlaceHolder)this.Master.Master.FindControl("yourContentPane");

Label myLabel = (Label)ph.FindControl("myLabel");
myLabel.Text = "Success!";

在VB.NET中

Dim ph As ContentPlaceHolder = DirectCast(Me.Master.Master.FindControl("yourContentPane"),ContentPlaceHolder)

Dim myLabel As Label = DirectCast(ph.FindControl("myLabel"),Label)
myLabel.Text = "Success!"

来自子页面的内容被加载到第一个母版页控件中,随后将其加载到祖父母母版页中.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读