MasterPages:Introduction(翻译)

(引用此译文者,请注明本出处)
dudu的blog曾经引用过原文,原文出处http://authors.aspalliance.com/PaulWilson/Articles/?id=13
现在,VS2005中已含有现成的MasterPages控件

概览
虽然确有其它一些page template技术可供使用,而且也支持runat=server下form的技术,但MasterPages却是唯一由微软ASP.NET小组开发的应用在ASP.NET下的page template的解决方法。Rob Howard,asp.net的设计专家,谈及page templates时,说相似的技术将会出现在asp.net下一版本中。本文讨论的是微软早期公布的MasterPages演示实例,我的下一遍文章,对此技术做出改进,提供了设计时支持的功能。

MasterPages是什么?
MasterPages包含一套服务器控件,你可以在微软的asp.net网站上的控件介绍里下载得到,还含有一个简单的演示例子。有别于其它的page template技术,它使用控件实现,代替了必须一开始就要编译生成自定义的页面基础类。设计者可以很容易地生成用户控件作为模板使用,标记为“Region”,然后在含有“ControlContainer”控件的页面增加匹配Region的“Content”控件。注意任何代码都无需编写,只要使用MasterPages控件就好了。

产生一个模板
要产生一个模板,首先你要产生一个让其它页面共享的asp.net用户控件,加入通用页面布局于其中。然后在页面布局中给出标识,标识此处需要由个别页面定义,由带有唯一ID的Region控件来标识,此ID往后用于匹配。只要你喜欢,你可以添加多个ContentRegion控件,而且能够添加普通的无需匹配的ID的Content控件进来。
                                                        例1:产生一个模板

<%@ 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>

使用这个模板
要使用这个模板,首先你要先建立一个空的asp.net页面,删除所有向导自动生成的代码。然后添加MasterPages的“ContentContainer”控件和“Content”控件,ContentContainer控件含有MasterPageFile属性,Content控件带有ID属性,匹配不同的Region控件的ID。你只要做的一件事是,把匹配的Content控件加入到ContentContainer中去,但如果你喜欢使用默认的Content控件的方式的话,你就可以跳过匹配Region的做法。
                                                            例2:使用这个模板

<%@ 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>

为什么使用MasterPages?
为什么要使用MatetrPages而非另外的Page Template技术呢?第一,此方式什么灵活,因为它能够自动支持多个Region,或是使用默认Content的可选Region,封装性更好,模板重用性高。它也更易使用,因为你只需使用控件实现而不用写代码了,更适合融入ASP.NET的页面-控件模型,让开发者解放出来编写另外的非GUI的功能性页面基类。最后,它日后很可能会成为ASP.NET模板的官方方法。

MasterPages的问题
不幸的是,现在的MaterPages只是一个演示例子。意味着还没有设计时支持功能,你还不能像其它控件那样可以拖放Region和Content控件,你必须手动添加。Regions、 Content都不具有可视化l。你也会发现NamingContainer技术带来了灵活性的同时,也使控件ID形如Container:_ctl0:Region:Control。这种控件布局会减慢速度。

结论
MasterPages是这么多种Page Template技术中最灵活和最容易使用的,它既然是由微软开发,将来很可能出现在ASP.NET中。最初的演示缺少设计时支持,性能差,但我的下一编文章会告诉你如何解决该问题,而且我也会增加定义默认的Content Region和默认模板文件的功能,以及如何解决曾经令人苦恼的NamingContainer问题。最后,到我的主页你会找到一些可选择的生成模板的演示例子。

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.


下一篇文章见:
MasterPages:Improved Version(翻译)
原文地址:https://www.cnblogs.com/lwj/p/288058.html