How do I reference controls in a nested master page from the content page code-behind?

Posted on 2009-02-20
Last Modified: 2013-12-14
I have a master page and several nested master pages in an ASP.Net 2.0/C# site. I have been able to reference controls in the master page from the various content pages by using the following...


Now I have a control in a nested master of the master page that I want to set the class value of a DIV control based upon which content page is currently displayed.

Master Page--->Nested Master Page --> DIV on nested master page  needs to be referenced from a content page that uses the nested master page (Master-->Nested Master-->Content page).

I tried the following code in the content code-behind with no luck.




Does anyone know how to reference the DIV control in the nested master?
Question by:dcadler
    LVL 8

    Accepted Solution

    You can find the control on nester master page is like this.

    HtmlGenericControl div = (HtmlGenericControl)this.Master.Master.FindControl("ContentPlaceHolder1").FindControl("objBody");
            div.Attributes.Add("Class", "cssadmin");

    Where  ContentPlaceHolder1 is the id of the content place holder of main master page.

    Note: Must have the runat ="server" for div on nested master page.


    Author Closing Comment

    Thanks. This worked exactly as I had hoped.Good job!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
    The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

    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

    12 Experts available now in Live!

    Get 1:1 Help Now