Welcome to AspAdvice Sign in | Join | Help

Orcs Goblins and .NET

I enjoy reading and writing. I hope you enjoy at least the former.

Accessing Master Page Properties from a content page

As I mentioned in my previous blog post about Accessing a property of a base page from a user control, I am going to explain how to access a property on a MasterPage from the content page. One merely has to check the namespace and the class name of the masterpage, which can be found in the code behind file. Just cast the Content Page’s Master as the class of the masterpage file which it uses, and then just access the value. It is really quite simple.

int neededValue = ((MyNameSpace.MyMasterPageClassName)Master).MyProperty;

Using that method you are able to easily access a property of a masterpage file when needed.

Sponsor

Published Thursday, June 21, 2007 1:05 PM by Brendan

Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Accessing Master Page Properties from a content page @ Friday, June 22, 2007 3:22 AM

You can easily add:

<%@ MasterType VirtualPath="~/MasterPage.master" %>

Just below the Page directive and then you can access the proeprty as:

int neededValue = Master.MyProperty;

Regards

Bilal Haidar [MVP]

# re: Accessing Master Page Properties from a content page @ Friday, June 22, 2007 9:19 AM

Cool thanks, Bilal.

Do you know if it is possible to do the same thing using nested MasterPages?

Brendan

# re: Accessing Master Page Properties from a content page @ Friday, June 22, 2007 11:34 AM

I just tested this with nested masterpages. It seems that it will work with them as Master.Master.MyProperty as long as both masterpages have <%@ MasterType VirtualPath="~/MasterPage.master" %>

delcared.

It seems that casting the masterpage also works with nested masterpages.

Brendan

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below