views:

270

answers:

4

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?

+1  A: 

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";
Kevin
The content pages have the following:<%@ MasterType VirtualPath="~/Shared/MyMaster.master" %>They don't have the TypeName attribute set. I assume I have to do this?
A: 

You can use

Page.Master

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

zwanz0r
A: 

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

Aaron Daniels
A: 

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,

prsthapit
Is DefaultMaster the value of TypeName? If so, please see my comment for Kevin earlier.