views:

129

answers:

4

I may be nuts, but master pages scare me because I fear that once I am locked into using a master page, I will encounter a situation where I will want to inherit only 90% of the visual content of the master page, forcing me to break the inheritance and thus having to reproduce the content that was in the master and bring it into the child.

I sense that this is a problem with any kind of "inheritance" in that you have to be careful, but it seems that at least you can override methods in a regular class.

I'm probably not too coherent here, but user controls seem to give you more flexibility. The only draw back is that you have to drag them onto a webform. To that, I say big deal.

Converting my app from using usercontrols to master pages is scaring me and I am afraid that my Javascript will break.

Comments?

+1  A: 

Like you mentioned, inheritance problems can appear from anywhere. Don't over engineer early.

With the masterpages problem you describe, you can swap in/out different css files and use selectors to hide/change parts of the html that is generated from the masterpage.

cottsak
+7  A: 

I wouldn't consider not using master pages. You can always nest master pages if you want to share the out most chrome on most pages, inner chrome to share in a silo etc.

Mark
+1 for mentioning nesting... that's what I would do.
Andy West
I guess what your saying is that, while you personally wouldn't use them, if you did, you would nest them to give yourself the most flexibility. I do't follow the inner chrome, silo point, but why wouldn't you use them?
Velika
So let's say I wanted a common header and stylesheet on every page; I would have a main master page. Then in a specific area of my site, say my blog area, I would have another master page that would have the main master page outside and page content inside. I can nest master pages inside of other master pages to share varying degrees of similar content across several different pages.
Mark
Also, I was saying I would always use them, unless there is another templating scheme already in place.
Mark
+11  A: 

Don't be scared.

You can create as many placeholders as you like, making your master page very granular. These placeholders can also contain default content & controls.

Therefore in the 90% of pages where you want the default markup you can omit the overriding content.

Alternatively in the 10% of cases where you want something different you can supply overriding markup

Example:

<%@ Master Language="C#" %>
<html>
<body>
<asp:ContentPlaceholder id="Headline" runat="Server">
    My Default Headline
</asp:ContentPlaceholder>
<asp:ContentPlaceholder id="Main" runat="Server" />
</body>
</html>

on your homepage you could have a page like so:

<%@ Page MasterPageFile="..." %>
<asp:Content ContentPlaceHolderID="Headline" runat="Server">
    My homepage headline
</asp:Content>

<asp:Content contentplaceholderid="Main" runat="server">
    My homepage main content
</asp:Content>

on all your other pages take advantage of the default 'Headline' by omitting the tag for the headline like so:

<%@ Page MasterPageFile="..." %>
<asp:Content contentplaceholderid="Main" runat="server">
    My page main content
</asp:Content>
Myster
+1  A: 

Once you're used to them, you'll start using them as intended. That information (refs to CSS,common js, page placement concerns, etc.) that is shared by enough pages to warrant a common source will go there, and you'll be happy later that they are there.

When you build out a page that breaks from that, you may find yourself doing a little adjusting back at your main.master (abstracting things differently), but you'll be able to make the adjustment fast and move forward.

If it's just a one-off page that breaks the rules, then you can simply tell that page to inherit a different master.

Make a test site and play around with the concept a bit before you start relying on it however.

madcolor