Master Pages in ASP.NET 2.0

Posted by: Clarity Blogs: ASP.NET, on 22 Dec 2005 | View original | NEW Bookmarked: 0 time(s)

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

Master Page Added

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.

Master Page Added

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.

Advertisement
Category: ASP.NET | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1779 | Hits: 42

Similar Posts

  • New article: How to detect and avoid memory and resources leaks in .NET applications more
  • Sneak Peek: ASP.NET Splitter Control more
  • Gaia Ajax 3.6 Alpha released: Ajax GridView and Adaptive Rendering ++ more
  • Announcing the Microsoft AJAX CDN more
  • Sneak Peak: CSS Sprites Make Your Websites Faster more
  • No JavaScript IntelliSense in VS 2010 Beta 2? Reset your Settings more
  • New location (and look) for ASP.NET documentation more
  • Dovetail is Hiring a Junior-to-Mid-level .NET Developer more
  • ClientIDMode in ASP.NET 4.0 more
  • Aspose.Total for .NET Q3 2009 Released more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD