Reputation: 1418
I have got a master page, a nested master page and a content page: Master(Site.master):
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
</head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false"
IncludeStyleBlock="true" Orientation="Horizontal" RenderingMode="List">
<StaticSelectedStyle BackColor="LightBlue" BorderStyle="Solid" BorderColor="Black"
BorderWidth="1" />
<Items>
<asp:MenuItem NavigateUrl="~/xxx.aspx" Text="xxx" />
<asp:MenuItem NavigateUrl="~/xxx/xxx/xxx.aspx" Text="xxx" />
</Items>
</asp:Menu>
<div class="main">
<asp:ContentPlaceHolder ID="cuerpo" runat="server" />
</div>
</form>
</body>
</html>
Nested master page(mOperator.master)
<%@ Master Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="mOperator.master.cs"
Inherits="aplicacion_operadores_mOperador" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cuerpo" runat="Server">
<div class="clear hideSkiplink" id="capaMenu">
<asp:Menu ID="subMenuOperator" runat="server" CssClass="menu" EnableViewState="false"
IncludeStyleBlock="true" Orientation="Vertical" RenderingMode="List">
<StaticSelectedStyle BackColor="LightBlue" BorderStyle="Solid" BorderColor="Black"
BorderWidth="1" />
<Items>
<asp:MenuItem NavigateUrl="~/yyyy.aspx" Text="yyy" />
<asp:MenuItem NavigateUrl="~/yyy/yyy/yyyy.aspx" Text="yyyy" />
</Items>
</asp:Menu>
</div>
<asp:ContentPlaceHolder ID="masterRight" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
Content page:
<%@ Page Title="" Language="C#" MasterPageFile="~/yyy/yyyy/mOperator.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="operators_Default" %>
<asp:Content ID="Content1" ContentPlaceHolderID="masterRight" Runat="Server">
</asp:Content>
Now, if I want to access the menu of the master page from content:
Menu miPrincipal = (Menu)Master.Master.FindControl("NavigationMenu");
miPrincipal.Items[1].Selected = true;
I get the value succesfully. but...
If I want to access the menu of nested master page. I am trying like this:
Menu miSecundario = (Menu)Master.FindControl("subMenuOperator");
miSecundario.Items[1].Selected = true;
But is giving me null.
Any ideas?
Upvotes: 0
Views: 1894
Reputation: 913
Try something like this
Menu mymenu = this.Page.Master.FindControl("cuerpo").FindControl("Content2").FindControl("subMenuOperator") as Menu;
Upvotes: 2
Reputation: 1189
You should use strongly typed Master Page
In your master page mOperator you expose the menu :
public Menu SubMenuOperator
{
get
{
return this.subMenuOperator;
}
}
Add the master type to the content page
<%@ MasterType VirtualPath="~/mOperator.master" %>
and then you can access to the menu with :
Menu menu = this.Master.SubMenuOperator;
Upvotes: 0
Reputation: 1418
I got it:
Menu mymenu = this.Page.Master.Master.FindControl("cuerpo").FindControl("subMenuOperator") as Menu;
Upvotes: 0