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?
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?
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";
You can use
Page.Master
You can than cast that property to your specific masterpage type.
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,