How to access controls inside a nested master page? why does it behave differently from content pages?

♀尐吖头ヾ 提交于 2019-12-04 05:27:58
R0nn1e
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");
  //base content place holder id

Label objLabel3 = (Label)cp.FindControl("lblNested");
  //lblNested is id in nested master page

I read few things here: http://www.odetocode.com/Articles/450.aspx and found out that the nested page in the middle never calls Page_Load! instead, it fires a load event that you can catch to set whatever fields, so the answer was in: on nested page do the following:

protected override void OnLoad(EventArgs e)
    {
        myTextBox.Text = "anything";
        base.OnLoad(e);
    }

This should work without any problems, so something else must be wrong. I just tried it inside a simple test project and I have no problems finding a control on the master page in both cases.

I would check (again) if you refer to the correct master page inside your nested master page. What you could also check is the runtime type of the Master property inside your nested master page. It should be the type of your master page.

EDIT: I thought the issue was about finding a control in the root master page from a nested master page and this should work without any problems. For finding a control inside a content placeholder in a nested master page, take a look at the following forum post.

You can have absolute control of your content in both master page and nested page from your content page using the directives:

<%@ MasterType VirtualPath="your_master.master" %>
<%@ Reference VirtualPath="~/your_master.master" %>

See the excellent article from K. Scott Allen in Ode To Code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!