Are master pages the way to go?
Asked Answered
I

5

5

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?

Isis answered 25/11, 2009 at 20:55 Comment(0)
C
13

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>
Castorina answered 25/11, 2009 at 21:10 Comment(0)
F
8

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.

Foreordain answered 25/11, 2009 at 20:59 Comment(3)
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?Isis
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.Foreordain
Also, I was saying I would always use them, unless there is another templating scheme already in place.Foreordain
N
1

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.

Nevlin answered 25/11, 2009 at 20:59 Comment(0)
P
1

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.

Patriotism answered 25/11, 2009 at 21:10 Comment(0)
G
-2

The idea behind master page is nice (if not trivial) but it is useless because their realization.

  1. the master page does not call the page (content) but the opposite. So,what's the problem with it?. Simple, you can't call different pages but a single page. So, you are coding and putting all your code in the same page. It is a nonsense at all. Master page allow several content block but all point to the same page (hence, cluttering the code).

  2. It is not trivial to connect the Master Page code and the caller Page, for example, they share the same "form" tag, but you must put the form tag in your page and you can use the form tag only once. :-?

  3. Variables/Values are not shared directly, you can but it require some "hack" (calling a function inside the master for example)

  4. You must pay attention in the loading cycle (before the page load ).

Master Pages are nice, if your master page is static, otherwise it is way better to simple "copy & paste" in every page or to use custom controls, because master pages add a new level of complexity.

Remember than the goal of Master Page is to save time, if it fails in this task then, it is useless.

my 2 cents.

Gallego answered 6/7, 2011 at 20:13 Comment(2)
Are you aware that you actually copy/pasted the same section of text three times?Rhatany
-1 I don't believe point 1 is correct, piont 2, you can't put 2 forms which runat server on any asp page, regardless of whether you use masterpages, 3. true, separation of concerns is a good thing. 4. very true.Castorina

© 2022 - 2024 — McMap. All rights reserved.