MasterPages: Introduction

MasterPages: Introduction

Paul Wilson
www.WilsonDotNet.com
www.ASPAlliance.com/PaulWilson

Previous Article            Download Demo            Next Article

Overview

MasterPages is the only solution for Page Templates in ASP.NET developed by the Microsoft ASP.NET Team itself, although there are certainly several other Page Template techniques available, including some with server form support. Rob Howard, Program Manager for ASP.NET, speaking about MasterPages, stated "technology similar to the sample will be in the next version of ASP.NET". This article discusses the original MasterPages demo released by Microsoft; see my next article for improvements I've made, including designer support.

What Is MasterPages?

MasterPages is currently a set of server controls, along with a small demo, that is available online in the Control Gallery on the MS ASP.NET website. It is very different than other Page Template methods since it uses controls, instead of a custom base page class that must first be created and compiled. Designers simply create a User Control as the Template, mark it with "Regions", and then add matching "Content" regions in the page "ContentContainer" control. Note that no code needs to be written, its just using the MasterPages controls.

Creating a Template

To create a MasterPages Template you start by creating an ASP.NET User Control and then add your common page layout that you intend all of your pages share. Then you mark any spots in the layout that will be defined in individual pages with a MasterPages "Region" control, along with a unique ID to be matched later. You can include as many Regions as you like, and you can add default content inside Regions that will be used for pages that do not contain a matching ID. Note the Register directive and control syntax is automatic with drag-n-drop.
Listing 1: Creating a Template
<%@ Control %>
<%@ Register TagPrefix="mp" Assembly="MasterPages" 
    Namespace="Microsoft.Web.Samples.MasterPages" %>
<html>
<head>
  <title>MasterPages</title>
</head>
<body>
  <h1><mp:region id="MPHeader" runat="server">Page Header</mp:region></h1>
<form id="frmMain" method="post" runat="server">
  <mp:region id="MPContent" runat="server">Default Content</mp:region>
</form>
  <h2><mp:region id="MPFooter" runat="server">Page Footer</mp:region></h2>
</body>
</html>

Using the Template

To use the MasterPage Template you start by creating an empty ASP.NET Page, stripping out all the standard content that the wizard automatically adds. Then you add the MasterPages "ContentContainer" control and "Content" controls, with the MasterPageFile attribute of the ContentContainer being the Template, and the IDs of the Content controls matching the various Region control IDs. The only thing you can add to the ContentContainer is matching Content controls, but you can skip some Regions if you prefer to accept their default content.
Listing 2: Using the Template
<%@ Page %>
<%@ Register TagPrefix="mp" Assembly="MasterPages" 
    Namespace="Microsoft.Web.Samples.MasterPages" %>
<mp:contentcontainer runat="server" masterpagefile="Template.ascx">
  <mp:content id="MPHeader" runat="server">Sample Page</mp:content>
  <mp:content id="MPContent" runat="server">Real Content</mp:content>
</mp:contentcontainer>

Why Use MasterPages?

So why should you use MasterPages instead of another Page Template technique? First, its the most flexible, since it automatically supports multiple regions, optional regions with default content, and even nested and repeating templates. Its also the easiest to use, since you just use controls without writing code, which makes it a natural fit into the rest of the ASP.NET page-control model, leaving your developers free to create non-GUI "functional" base page classes. Finally, it will likely someday be the official template method in ASP.NET.

MasterPages Problems

Unfortunately, the current MasterPages controls are just a demo at this time. This means there is almost no design-time support -- you cannot drag-n-drop any controls into the Region or Content controls -- you must add them manually. Your defaults are at least visible for Regions, but you see no Content at all. You will also find this technique is NamingContainer "happy" due to flexibility, but this means your controls will have IDs like Container:_ctl0:Region:Control. These extra control layers also cause this technique to be slower than others.

Conclusion

MasterPages is the most flexible and easiest to use of Page Template solutions, and since it was created by Microsoft it will likely someday be in ASP.NET itself. The original demo suffers from the lack of designer support and poor performance, but my next article will show you how to fix these problems, and add I will also add the ability to define your default content regions and default template files, while also cleaning up the ever-annoying NamingContainer "problem" that occurs. Finally see my website for a demo of making the templates user-selectable as well.

Author Bio

Paul Wilson is a software architect in Atlanta, currently with a medical device company. He specializes in Microsoft technologies, including .NET, C#, ASP, SQL, COM+, and VB. His WilsonWebForm Control allows Multiple Forms and Non-PostBack Forms in ASP.NET. He is a Microsoft MVP in ASP.NET and is also recognized as an ASPFriend's ASPAce/ASPElite. He is a moderator on Microsoft's ASP.NET Forums, as well as one of the top posters. He is certified in .NET (MCAD), as well as also holding the MCSD, MCDBA, and MCSE. Please visit his website, www.WilsonDotNet.com, or email him at Paul@WilsonDotNet.com.

From: http://authors.aspalliance.com/PaulWilson/Articles/?id=13

posted on 2004-02-17 20:13 dudu 阅读(2532) 评论(3)  编辑 收藏 网摘 所属分类: ASP.NET

评论

#1楼 2004-02-23 10:48 eng21

我觉得最好不要这样全部copy别人的文章,而又不注明原始出处,使用链接都可以。   回复  引用    

#2楼 2004-02-23 10:56 dudu

不好意思, 当时主要用于收藏的目的, 以便找起来方便。忘了注明出处, 这是我的疏忽, 真抱歉!   回复  引用    

#3楼 2006-04-03 17:18 800电话防伪 语音自动应答 0551-5336491 [未注册用户]

  回复  引用    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1314




相关文章:

相关链接:

导航

公告

人生的真正价值在于从何种程度与何种意义上摆脱自我!
明天继续更新评论功能
<2009年7月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

统计

与我联系

搜索

 

常用链接

留言簿

随笔分类

随笔档案

新闻分类

相册

HJ

朋友的博客

网站收藏

小组

友情链接

最新随笔

最新评论

阅读排行榜

评论排行榜

60天内阅读排行