• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 910
  • Last Modified:

Access control in nested master page template

I'm trying to access different controls that are located in both of my nested master page templates. Here is a little diagram to keep us all on the same page...

HeaderMaster.master <-- SubPageMaster.master <-- AboutUs.aspx

HeaderMaster.master contains <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> and SubPageMaster.master contains <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>.

My goal is to change the text of each Label from the AboutUs.aspx pageload sub routine.

This code CType(Master.FindControl("Label1"), Label).Text = "blah" seems to work fine when using just one template, but when using it in a nested setup I get this error: System.NullReferenceException: Object reference not set to an instance of an object.

Would someone please tell me how I can access the controls in each template?

1 Solution
oh well, one master page is bad enough.

try using trace="true" in the header and then check the full html name of the control you want eg $ctrl01$label1 and then use this in the findcontrol
graphicodysseyAuthor Commented:
I didn't use the full control name, but your comment of the Trace made me review the control structure a little more. What I found out was... even though the control is in the SubPageMaster file, it really lives in the Content Place Holder used in it's parent. So to get to a control on the SubPage, you need to go to the very top and work your way down, which would be... Master.Master.BodyContent.Label2 (where BodyContent is the ID of the ContentPlaceHolder control in the HeaderMaster file). To access a control within the HeaderMaster, you would simply use Master.Master.Label1. If you wanted to access a control within a ContentPlaceHolder control on the AboutUs page, you would need something like Master.Master.BodyContent.SubContent.Label3.

In any case, thanks for your assistance... it pointed me in the right direction.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now