Nick
Nick

Reputation: 7525

Get reference to the Master Page from the base class of content pages

I have a few content pages that inherit from BasePage and use a Master Page.

From the BasePage.cs file, I need to call a method defined in the Master Page.

How would I do it?

Upvotes: 0

Views: 2900

Answers (4)

prsthapit
prsthapit

Reputation: 85

Lets say you have a property given below to enable header in Master Page:

Master Page Code:

Public Property EnablePageHeader() As Boolean
    Get
        If ViewState("EnablePageHeader") Is Nothing Then
            ViewState("EnablePageHeader") = True
        End If
        Return DirectCast(ViewState("EnablePageHeader"), Boolean)
    End Get
    Set(ByVal value As Boolean)
        ViewState("EnablePageHeader") = value
    End Set
End Property

Now if you want to call this Property from any other base class or any other page then you can write code as follow:

DirectCast(Master, DefaultMaster).EnablePageHeader = False

Hope similar is the case of Methods too.

Please respond if the code above helped you are if there is any mistake in it.

Thanks,

Upvotes: 0

Aaron Daniels
Aaron Daniels

Reputation: 9664

You can use Strongly Typed Master Pages. Also, see here.

Upvotes: 0

nicojs
nicojs

Reputation: 2055

You can use

Page.Master

You can than cast that property to your specific masterpage type.

Upvotes: 0

kemiller2002
kemiller2002

Reputation: 115538

This should do it:

    var masterPage = ((MasterPageType)Master);
or to access the function:
    ((MasterPageType)Master).SomeFunction();

You might have to set the master page file in your base page programmatically as well. We do it in the OnPreInit function.

this.MasterPageFile = "~/masterPage.master";

Upvotes: 1

Related Questions