tags:

views:

655

answers:

3

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?

A: 

In VB.Net

MasterPageVariable = Ctype(page.MasterPage, MasterPageClass)
Stephen Wrighton
+2  A: 

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.

FlySwat
+6  A: 

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.

Jason Stevenson
This is the easiest way to access your specific master page type through Page.Master, instead of going with the BasePage class method.
Redbeard 0x0A

related questions