I have a admin.master master page that is shared by several content pages. On the master page, I have a set of side-bar menus that get loaded dynamically from a xml file via a WebuserControl, this part of codes look like this:
<div id="LeftSideMenu" style="height:100%;">
<uc3:SideMenuXml ID="mnuCalendar" runat="server" MenuName="Calendar" /> <uc3:SideMenuXml ID="mnuUser" runat="server" MenuName="User" />
</div>
In the SideMenuXml.ascx, codes behind load xml content properly based on the MenuName which corresponds to a segment in the xml file that looks like this:
<Sidebar>
<Calendar>
<Name>School Calendar</Name>
<Url>student/SchoolCalendar.aspx</Url>
<Description>View school calendar</Description>
<DisplayOrder>1</DisplayOrder>
<Roles>Public</Roles>
</Calendar>
</Sidebar>
This has been working great, but sometimes I want a different set of side menus loaded into admin.master based on the purpose of the content page. Today, I learned that this can be accomplished by these steps:
- Create a public property in the master page, named “PageType” or whatever
- To access this property of master page, I needed to add this attribute to the content page, <%@ MasterType VirtualPath=”~/Admin.master” %>
- Then on the Page_Init event of my content page, I set this property to an enum value I wanted: protected void Page_Init(object sender, EventArgs e) { Master.PageType = MasterPageTypes.Contest;
} - Then on the admin.master I added this line of server-client-mixed code:
<div id="LeftSideMenu" style="height:100%;">
<% if (PageType== MasterPageTypes.Contest) {%>
<uc3:SideMenuXml ID="mnuContest" runat="server" MenuName="Contest" />
<% } %>
With that, I accomplished the goal of showing certain Contest related side menu for those content pages that set the PageType property of admin.master master page.
To sum it up, public property of master page is not accessible from its content page until you add this MasterType directive to the content page that uses the master: <%@ MasterType VirtualPath=”~/Admin.master” %>