Results 1 to 8 of 8

Thread: [RESOLVED] How to access to a control in nested master page

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Resolved [RESOLVED] How to access to a control in nested master page

    Hi everyone, I've a very strange problem!

    There are 2 master page in my web project:
    the first one is MasterPageA.Master which has some controls; the second one is MasterPageB.Master which is son of MasterPageA and it has some other controls.
    There is also another page, named Default1.aspx, which is son of MasterPageB.Master.

    I need to access to a label control put in the Content part of MasterPageB.Master.

    I tried to fit the code that I usually use for one level (Master-Son) found at this page and I've write in Default.aspx.vb this code:
    Code:
        Dim oContent As Content, oLabel as Label
          oContent = CType(Master.FindControl("Content1"), Content)
          If Not oContent Is Nothing Then
            oLabel = CType(oContent.FindControl("lblContatti"), Label)
          Endif
    These are the definitions (just the relevant code) of MasterPageA.Master and MasterPageB.Master:
    MasterPageA.Master
    Code:
    ...cut...
                <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
                </asp:ContentPlaceHolder>
    ...cut....
    MasterPageB.Master
    Code:
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
                            <asp:Label ID="lblContatti" runat="server" Text="E-Mail" CssClass="lblSchedaArticolo"></asp:Label>
    </asp:Content>
    ....
    ....
    Any help is appreciated.
    Best regards

    Maurice

  2. #2

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: How to access to a control in nested master page

    Hi Gary,
    I've found the article during my search on the Internet.
    Imagine that in the second master page I've this situation:
    Code:
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat=server>
    </asp:ContentPlaceHolder>
    </asp:Content>
    and in the content page (default.aspx), I need to access at the label control.
    I hope my explanation is clearer than the same of the first post!
    Thank you

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: How to access to a control in nested master page

    Hey,

    The technique is the same though. That label lives within the ContentPlaceHolder of the First Master Page, so from the actual content page, you have to go up through two master pages, and then start to come down again. This is code that I have just used, and know works.

    First Master Page
    Code:
    <&#37;@ Master Language="VB" AutoEventWireup="false" CodeBehind="MasterPage.master.vb" Inherits="NestedMasterPages.MasterPage" %>
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Outer Master Page</title>
        <asp:ContentPlaceHolder ID="head" runat="server">
        </asp:ContentPlaceHolder>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h3>Outer Master Page</h3>
            <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
            </asp:ContentPlaceHolder>
        </div>
        </form>
    </body>
    </html>
    Second Master Page
    Code:
    <%@ Master Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeBehind="MasterPage2.master.vb" Inherits="NestedMasterPages.MasterPage2" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" 
    Runat="Server">
    <h3>Inner Master Page</h3>
    <%-- This is the label I assume you are referring to --%>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat=server>
    </asp:ContentPlaceHolder>
    </asp:Content>
    Content Page
    Code:
    <%@ Page Language="vb"  MasterPageFile="~/MasterPage2.master" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="NestedMasterPages._Default" %>
    
    <asp:Content ID="Content1" runat="server" 
    contentplaceholderid="ContentPlaceHolder2">
    <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    <asp:Button ID="Button1" runat="server" Text="Button" />
    </asp:Content>
    Code Behind
    Code:
    Public Partial Class _Default
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            ' Outer Master Page
            Dim Outer_CP As ContentPlaceHolder
            Outer_CP = TryCast(Me.Master.Master.FindControl("ContentPlaceHolder1"),  _
            ContentPlaceHolder)
    
            Dim lbl As Label = TryCast(Outer_CP.FindControl("Label1"), Label)
    
            lbl.Text = "Text was changed inside nested master page!"
        End Sub
    End Class
    Hope that helps!!

    Gary

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Location
    Italy
    Posts
    117

    Re: How to access to a control in nested master page

    Hi Gary,
    it works well...

    Thank's a lot!

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] How to access to a control in nested master page

    Hey,

    Not a problem at all. Happy to help!

    Gary

  7. #7
    New Member
    Join Date
    Apr 2011
    Posts
    2

    Re: [RESOLVED] How to access to a control in nested master page

    thx .. it works ...

  8. #8
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: [RESOLVED] How to access to a control in nested master page

    Quote Originally Posted by furqanbutt View Post
    thx .. it works ...
    Wow, this is an older thread! Glad that it helped you though!

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width