Access control in nested master page template

Posted on 2006-06-05
Last Modified: 2012-08-14
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?

Question by:graphicodyssey
    LVL 12

    Accepted 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
    LVL 1

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now