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?
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>
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With