Click here to Skip to main content
15,897,187 members
Articles / Web Development / ASP.NET
Tip/Trick

How to Access Sub Master Page WebControl and Super Master Page WebControl on Content Page in Nesting of Master Page

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
23 May 2014CPOL 7K   3  
Access Web Control on Content Page of Nested Master Page

Introduction

Sometimes, a scenario arises when we need to control the navigation of a page which is inherit Super Master Page and then Sub Master Page in nesting of Master Page. Then how do we access the Super Master Page web control and Sub Master Pager web control. Here are some tips.

Using the Code

Suppose we have a Master page named A and Master page B and then a Content Page.

Content Page inherits MasterPageB and MasterPageB inherits MasterPageA.

Now there is a navigation bar in MasterPageA and one in MasterPageB.

To access the li of navigation of MasterPageB from ContentPage who directly inherits MasterPageB, the code is:

The li should be runat="Server" and also had an id

C++
ContentPlaceHolder cp = (ContentPlaceHolder)this.Master.Master.FindControl("ContentPlaceHolder1");

        System.Web.UI.HtmlControls.HtmlGenericControl liDepartment = 
    (System.Web.UI.HtmlControls.HtmlGenericControl)cp.FindControl("liDepartment"); 
liDepartment.Attributes.Add("class", "active");

To access the li of MasterPageA, the code is:

C++
 System.Web.UI.HtmlControls.HtmlGenericControl liDashboard = 
  (System.Web.UI.HtmlControls.HtmlGenericControl)this.Master.Master.FindControl("liDashboard");
 liDashboard.Attributes.Add("class", "active");
Hope this will work for you...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --