Accessing properties from nested master pages

S

Steve Franks

I have this cool nested master page scenario working great. However what is
the correct way to be able to access a strongly typed property at the top
level master from a content page that has a nested master page between the
content page and the top level master?

For example, assume the very top level master is called CompanyWide.master.
And the nested master page is called DepartmentA.master, which inherits from
CompanyWide master. Last I have a content page test.aspx that is created
with DepartmentA as its master page.

OK so now from the test.aspx page's code behind I want to set a public
property defined in CompanyWide master. However even though the property is
Public in CompanyWide master, I am not sure how to access it since there is
another master page in between it (DepartmentA.master).

What is the right way to do this?

Do I have to recreate these public properties in DepartmentA to act as a
stub, so I then call them from test.aspx and it takes them in the setter and
then sets them in the master? That seems quite messy - wouldn't think that
would be the way.

So I thought that maybe DepartmentA master would inherit CompanyWide
master's public properties since DepartmentA is a nested master of it. But
when I try:
(DepartmentA).this.Master I do not see the properties in CompanyWide's so
it doesn't look like they were inherited. And I cannot do this:
(CompanyWide).this.Master because test.aspx does not know what type
"CompanyWide" is.

Can someone please point me in the right direction here?

Thanks,

Steve
 
G

Graham

You should be able to use ((DepartmentA)this.Master).Property or
(this.Master as DepartmentA).Property
 
S

Steve Franks

I thought so too and had tried that before posting. It does not work
though.

Steve
 
S

Steve Franks

OK, I figured it out. Here it is:
((CompanyWide)this.Master.Page.Master).HtmlTitle = "now it is working";

Be sure you do NOT have a <%@ MasterType VirtualPath="~/whatever.master" %>
declared or you will get an InvalidCastException thrown at runtime.

I hope this helps someone else one day.

Steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top