Master Pages in ASP.NET 2.0
So I've changed my blog layout again...just gives you another reason to check back, doesn't it?
I've been fortunate enough to be working on the new Resource Kit DVD for Visual Studio 2005, and I've been seeing some really great content coming in, which should be a nice kick off for asp.net 2.0 developers. It has inspired me to refocus on my asp.net skills, so over the next few weeks I'll be honing in on specific topics that I think are really helpful to web developers.
Master Pages are new to asp.net, and along with themes & skins have really begun to remove the tedious tasks web devs have faced for years. Everyone has at some point found themselves editing UI properties in Copy/Paste hell, changing the same values in multiple places. This can happen when menu requirements change, UI designs are updated, or new content is needed. Master Pages are another weapon against this type of tedious task, allowing you to focus more on writing valuable code. Here's how they work:
Add a Master Page item to your web project. You'll see an <asp:contentplaceholder> item placed in the master page's source. This control will have an ID attribute, used to reference the control in the child pages, and the ubiquitous runat="server" attribute. Don't add any content inside, this will hold all content from the content (child) pages you are about to define. Add/Edit the Master Page as you normally would a web page, but keep in mind that this will be the core layout for all pages in this Master/Child family. Many websites are based loosely on the box model, if you want this type of layout add your header, footer and side menus here. This leaves a nice tidy container for content to slide inside

Right-click the MasterPage.master file in the Solution Explorer, and select 'Add Content Page'. Create as many content pages as you need. VS2005 does a great of handling the interaction between the pages, even showing your content pages via Intellisense when you add a hyperlink.

Look at your content page's HTML. Notice anything different? You should...the content page will only contain, (and only allow) code inside of the <asp:contentplaceholder> control. That should make sense, this page will inherit the Master Page's code for everything else. There are methods for adding page specific code inside of a content page; one example I'll be examining later is how to add JavaScript for one content page without including unnecessary functions for all other content pages.
This setup allows you to keep all of your menu items, header and footer designs centralized, without duplicating code. Keep in mind the Master Page can have multiple contentplaceholder controls...if a particular page doesn't want to use it, that's fine, just don't use it in your content page.
Happy webbing.