locked
Nested master pages and Master.FindControl RRS feed

  • Question

  • User361502242 posted

    Hi,

    I've a little problem with nested master pages.

    I've a content page which masterpage is nested into another masterpage. On the nested masterpage I've controls in <asp:Content> object and I need to access one of these controls from content page, but this.Master.FindControl returns null....

    Can anyone help me?

    Thanks in advance... R.

    Wednesday, January 3, 2007 4:29 AM

Answers

  • User-1859264798 posted

    I think the following have a better chance of working:

    ------------------------------

       ContentPlaceHolder cp = this.Master.Master.FindControl("MainContent") as ContentPlaceHolder;
        TextBox tb = cp.FindControl("textbox1") as TextBox;

    ------------------------------

    where your main(topmost) masterpage has a contentplaceholder like:

    <asp:ContentPlaceHolder ID="MainContent" runat="server" />
     
    Hope this helps. 


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2007 6:42 AM

All replies

  • User-1859264798 posted

    Hi,

    Try this:

    --------------------------------

               //use id of your content <asp:Content id="content1" ...>
                Content ct = this.Master.FindControl("content1") as Content;

                 //now find control's inside that content
                TextBox tb = ct.FindControl("textbox1") as TextBox;

    ----------------------------------- 

    Hope this gives the idea. 

    Wednesday, January 3, 2007 6:31 AM
  • User-1859264798 posted

    I think the following have a better chance of working:

    ------------------------------

       ContentPlaceHolder cp = this.Master.Master.FindControl("MainContent") as ContentPlaceHolder;
        TextBox tb = cp.FindControl("textbox1") as TextBox;

    ------------------------------

    where your main(topmost) masterpage has a contentplaceholder like:

    <asp:ContentPlaceHolder ID="MainContent" runat="server" />
     
    Hope this helps. 


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 3, 2007 6:42 AM
  • User361502242 posted
    Yeah :-) the second solution works :-) Thanks a lot.
    Wednesday, January 3, 2007 7:09 AM
  • User2133758077 posted

    Thank you thank you thank you!! 

    Wednesday, December 2, 2009 8:48 PM
  • User1577693665 posted

    It worked for me too!!! Thanks a lot.. 

    Tuesday, August 17, 2010 12:43 PM
  • User-658538320 posted

    A Lot of good info - http://cyberbrutus.com

    Thanks

    Tuesday, August 17, 2010 4:36 PM
  • User-931283679 posted

    And me!  Nice one.

    Monday, October 11, 2010 3:16 PM
  • User-414084354 posted

    I am really trying to do something very similar but by adding a master page content control on load as I am having problem with my templates rendering correctly.

    Here is my recent post posted on a well know forum however had no reply. Can someone help?

    hi all,
    I am currently using nested masterpages in my latest asp.net 4.0 project and i want to change the look and feel dynamically, so the homepage may just be 1 column wide, about us page may be 2 columns etc.

    I am dynamically loading the masterpage dependant on the record selected in the DB (1column.master, 2column.master) on the pre_init event of a static page (Load.aspx) which work succesfully however i need to be able to dynamically add a master page content to the page in order to get the desired layout.

    Is this possible, is there a better way of achieveing this?

    Please inform me if you need more information / or have any questions.
    Thanks

    Monday, October 11, 2010 3:54 PM
  • User2072126009 posted

    My master page file name is Site.Maste and i write the following lines of code it generates error:

    "Object reference not set to an instance of an object" 

     

     

    ContentPlaceHolder cp = this.Master.Master.FindControl("MainContent") as ContentPlaceHolder;

                TextBox tb = cp.FindControl("test") as TextBox;

     

    any one have solution help me.

    THnx in advance.

    Tuesday, March 15, 2011 7:43 AM
  • User-1164658185 posted

    I have to get the value from content

    ContentPlaceHolder cp = this.Master.Master.FindControl("cph1") as ContentPlaceHolder; 
    Content ct1 = cp.FindControl("cph1") as Content; ---> returns null
    DropDownList ddlAge1 = cp.FindControl("age1") as DropDownList;
     
     

     

    Wednesday, June 8, 2011 1:25 PM
  • User1014298547 posted

    This didn't work for me. My parent master page content ID happens to have the same name as yours. However, I am referencing a hyperlink with ID="hlShipClasses", where I'm trying to dynamically change the text properity.

    Here's the code:

            ContentPlaceHolder cp = this.Master.Master.FindControl("MainContent") as ContentPlaceHolder;
            HyperLink hlink = cp.FindControl("hlShipClasses") as HyperLink;
            hlink.Text = "Test";

    When I run it, I get an exception error:


    System.NullReferenceException: Object reference not set to an instance of an object.


    I looked at cp with the debugger and noticed it was null, so I assume that means the control was not found. So I tried this:

            ContentPlaceHolder parentCP = this.Master.Master.FindControl("MainContent") as ContentPlaceHolder;
            ContentPlaceHolder childCP = parentCP.FindControl("CGMapMainContent") as ContentPlaceHolder;
            HyperLink hlink = childCP.FindControl("hlShipClasses") as HyperLink;
            hlink.Text = "Test";

    where CGMapMainContent is the content place holder in the child master page. It works, but the second line seems redundant. You have to go up through the hierarchy and then back down finding each control as you go. Is there an easier way to do this?

    Friday, October 21, 2011 12:13 PM