access members in the Master page class

B

Bob

In ASP.NET 1.1, I have a base page for all my pages in an application. The
base page contains some common properties that every page needs, the code
behind file of every page looks like this:

public class Home : MyBasePage {
.....
}

and in it I can simply refer them as base.MyProperty (or just
this.MyProperty). This seems no longer possible in the ASP.NET 20 master
page, as the code behind file for the master page is only a partial class.
There's no way to inherit from a partial class in the code behind file:

public partial class Home : MyMasterPage { //This does not work

}

If I simply add a new page assigning it the master page, any HTML code is
inherited but the members defined in the master page code behind file are
not recognized. Any way to make this work?

Thanks
Bob
 
T

Trevor Braun

Add the master file reference to your page markup:
<%@ MasterType VirtualPath="~/Default.master" %>

Then your page will have access to the master properties through your
inherited page's Master property.

Trevor Braun
 
B

Bob

Thanks a lot Steve, That's what I'm looking for. Though the extra type
cast is a bit hassle. I hope Microsoft would improve on the master page
concept to make it a true inheritance in the future.
 
J

Joerg Jooss

Thus wrote Bob,
Thanks a lot Steve, That's what I'm looking for. Though the extra
type cast is a bit hassle. I hope Microsoft would improve on the
master page concept to make it a true inheritance in the future.

It seems Steve missed the fact that you can get typesafe access to your Master
Page by specifying a MasterType directive in the content page, e.g.
<%@ MasterType VirtualPath="~/MasterPage.master" %>

Cheers,
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top