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

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...

            ((HtmlGenericControl)Master.Master.FindControl("objBody")).Attributes.Add("Class",
            "cssadmin");

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.

            ((HtmlGenericControl)Master.AdminNestedMaster.Master.FindControl("objDivMenu")).Attributes.Add("Class",
            "Admin-AccessLevels");


and...

            ((HtmlGenericControl)AdminNestedMaster.Master.FindControl("objDivMenu")).Attributes.Add("Class",
            "Admin-AccessLevels");


Does anyone know how to reference the DIV control in the nested master?
dcadlerAsked:
Who is Participating?
 
bedanandConnect With a Mentor Commented:
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.

0
 
dcadlerAuthor Commented:
Thanks. This worked exactly as I had hoped.Good job!
0
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.

All Courses

From novice to tech pro — start learning today.