I have a BasePage which inherits from System.Web.UI.Page and every page that inherits the BasePage will have the same master page.
How do I cast the Page.Master of the BasePage to the specific master page so I can access properties on it?
I have a BasePage which inherits from System.Web.UI.Page and every page that inherits the BasePage will have the same master page.
How do I cast the Page.Master of the BasePage to the specific master page so I can access properties on it?
In VB.Net
MasterPageVariable = Ctype(page.MasterPage, MasterPageClass)
Overriden Master can't be done (its not Virtual), and masking it with new causes an issue with the page class not being able to get its master, so the best thing to do is a second property.
Something like:
public CustomMasterPage MasterPage
{
get { return this.Master as CustomMasterPage; }
}
In your BasePage class.
A better way is to add the MasterType property to the pages that use that master. Then you can simply access the master page properties through the page object.
<%@ MasterType VirtualPath="~/site.master" %>
You just use this in your code:
this.Master.propertyName
To access the property of the master page for the current page.