Welcome to AspAdvice Sign in | Join | Help

Orcs Goblins and .NET

I enjoy reading and writing. I hope you enjoy at least the former. I have moved my blog to Brendan.Enrick.com.

Syndication

Tags

News

Locations of visitors to this page

Navigation

Archives

Advice Sites

Articles

Blogs

Music

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

# re: Accessing Master Page Properties from a content page @ Friday, September 05, 2008 11:47 AM

Thanks man!

Flashbeir

# Accessing Properties of a Base Page from a User Control @ Wednesday, May 06, 2009 2:17 PM

Earlier today I was helping someone who was working with a user control. That control was on an ASP.NET page which was inheriting from a base page. From the user control he could not access the properties of the base page. He mentioned that he was getting

Brendan Enrick's Blog

# Accessing Properties of a Base Page from a User Control @ Wednesday, May 06, 2009 2:32 PM

Earlier today I was helping someone who was working with a user control. That control was on an ASP.NET page which was inheriting from a base page. From the user control he could not access the properties of the base page. He mentioned that he was getting

Brendan Enrick's Blog

# Accessing Properties of a Base Page from a User Control @ Monday, July 26, 2010 3:17 PM

Accessing Properties of a Base Page from a User Control

Brendan Enrick

# Accessing Properties of a Base Page from a User Control @ Friday, July 30, 2010 10:17 AM

Accessing Properties of a Base Page from a User Control

Brendan Enrick

Leave a Comment

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