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