Search code examples
asp.netvb.netwebformsmaster-pages

How to access master page public property from a NESTED content page


After exposing a public property in Top.Master it can be accessed in any child page that has a master type reference on its page.

How can the same properties be accessed from a nested page?

I tried to cascade the properties down the heirarchy but the child page errors when trying to access it.

I would prefer to access the exposed top.master property directly from the nested content page but am unsure of a good way to do this.


TOP.MASTER

<asp:Label ID="lblMsg" ClientIDMode="Static" runat="Server" />

TOP.MASTER.VB

Partial Public Class TopMaster
  Inherits MasterPage

  Public Property Msg As String
    Get
      Return lblMsg.Text
    End Get
    Set(value As String)
      lblMsg.Text = value
    End Set
  End Property

End Class

CHILD.MASTER

<%@ MasterType VirtualPath="~/Top.Master" %>

CHILD.MASTER.VB

Master.Msg = "Success"

CHILD.PAGE

<%@ MasterType VirtualPath="~/Child.Master" %>

CHILD.PAGE.VB

Master.Master.Msg = "Success"

Solution

  • In your child.master class you can create a Msg property that would proxy the top master Msg property

    You can add the following code in child.master.vb

      Public Property Msg As String
        Get
          Return Master.Msg
        End Get
        Set(value As String)
          Master.Msg = value
        End Set
      End Property
    

    then in your child.page.vb you can access this property doing

    Master.Msg = "Success"