MasterPage inheriting a MasterPage... is it possible?

T

ThunderMusic

Hi,
Maybe there is another way of doing this, but here's my problem...

I have my web site where I have a master page to have a common layout and a
common behavior on all my pages (that's what it's intended for)... But now
here comes the time where some pages need some processing (like, is the user
logged? and those kind of things)... If it was only on 1 or 2 pages, I'd
says, i'll put it directly in the code and case closed, but there are 10-20
pages that need this behavior while 10-15 pages don't... So I'd inherit my
master page to include everything in the base masterpage into the new master
page that will do the additionnal processing... and use this inherited
master page in the pages that need the extra processing... Can it be done?

I've thought of another way... that would be to create some properties in
the master page code file and setting them in my "sub-pages" to know which
processing to use... I can set these properties fine in my "sub-pages" code,
but is it possible to set a Master Page's property right in the markup like
we do for user controls?

thanks a lot

ThunderMusic
 
G

Gregory Gadow

ThunderMusic said:
Hi,
Maybe there is another way of doing this, but here's my problem...

I have my web site where I have a master page to have a common layout and a
common behavior on all my pages (that's what it's intended for)... But now
here comes the time where some pages need some processing (like, is the user
logged? and those kind of things)... If it was only on 1 or 2 pages, I'd
says, i'll put it directly in the code and case closed, but there are 10-20
pages that need this behavior while 10-15 pages don't... So I'd inherit my
master page to include everything in the base masterpage into the new master
page that will do the additionnal processing... and use this inherited
master page in the pages that need the extra processing... Can it be done?

Yes, and I've done it successfully.

Master pages by default inherit from System.Web.UI.MasterPage. You can create a
custom class that inherits MasterPage, then use the Inherits attribute in the
<%@Master %> tag to base the master page on your custom class. You can then
reference the public methods and properties of the new class through the client
page's Master property. In, say, the client page's PreRender event, you might
have this code:

Dim MyMP as TechBear.Web.MasterPage = CType(Me.Master, TechBear.Web.MasterPage)

If MyMP IsNot Nothing Then
MyMP.Property1 = "This value"
MyMP.ListOfMyObjects.Add(New MyObject("value", 1))
MyMp.CallThisMethod()
End If

I have a MasterPage class that does all kinds of nifty things, like set a title,
subtitle and list of "see also" links at the top of the page, display or not
display a logout and print button, place page styles and javascripts into the
header block (meaning my pages will validate properly), and a few other useful
tricks.

For items that have no bearing on the master page itself, you migh consider
creating a custom class that inherits System.Web.UI.Page, which is the base
class for a client page. I have one of those, too, with functions that retrieve
page parameters, handles cookies, as well as properties that give access to my
custom MembershipProvider object and, if the user is logged in, to an instance
of my custom MembershipUser object with data on the logged in user. Again, just
add the Inherits attribute to the <%@Page %> tag; then you can use those custom
methods and properties through the page's Me object, so

If Me.MyUser.HasExecutiveRole Then...

or simply

If MyUser.HasExecutiveRole Then...
 

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

Top