ASP.net 2.0 Migrating系列 - Master Pages
范维肖
在Visual Web Developer 2005里的新建里多了一个Master Pages,在微软的VWD2005的Guided Tour里有专门对它使用的介绍,可见它是ASP.NET2.0里的一大特色.
最早我们在做HTML页面的时候,有时会有遇到这样的情况:一个网站要有一个统一的风格,所以页面的样式是大体一致的,因此我们都喜欢做一个包括导航栏在内的Model页面,然后每次都打开它,然后另存为像aboutus.html这样的页面,然后再编辑,再保存。后来东西多了,我们就开始用Frameset把页面分开,像导航菜单,版权声名部分都用专门的frame。到了ASP时代,通常也是把版面设计和asp代码分开。设计完版面再填写代码,对于有相似样式的页面,我们也是通过copy样式的html代码来实现的,后来为了适应skin技术,有人开始做一个model页面,里面需要插入ASP的部分用类似$BlogContent$的标记来表示,然后用程序再去分析。到了ASP.NET的时代,有时我们也尝试着用userControl来实现frameset那样的思想。这些我们的“土方法”最终在VWD2005结束了,这个新的feature相比是VWD2005的设计者也是饱受了痛苦之后才设计出来帮助大家的吧。
这里是微软Migrating系列里对master pages的介绍:
Master pages are a new feature introduced in ASP.NET 2.0 to help you reduce development time for Web applications by creating a single place to maintain a consistent look and feel in a site. Master pages allow you to create a template that can be used to generate a common layout for many pages in the application. The primary goal of master pages is to avoid creating each page from scratch and having to repeat the layout code. Another benefit of using master pages is that if you want to change the layout of the pages in the application, you only have to update the master page, rather than each individual page.
Master Pages是ASP.NET 2.0里介绍的一个新特色,通过建立一个来控制一致的样式的页面,可以帮助你缩短开发web应用程序的时间。Master Pages允许你建立一个模板,这个模板用来生成一个使程序中的页面(如果需要样式一致的)具有相同的布局。Master pages的主要目的是为了避免我们从一个已经建好的页面里挖出那些控制页面样式的代码,然后在重复它们的建立页面的方法。使用Master pages的另一个好处是如果你想改变程序中页面的样式的时候,你只需要来改变这个master page就可以了,不用再一个一个的单独修改了。
A master page looks like any ordinary ASP.NET Web page except for the extension (.master instead of .aspx) and some special controls and header fields. Master pages must contain one or more <asp:ContentPlaceHolder> controls. These controls represent areas of replaceable content. Basically, anything that is not in a ContentPlaceHolder will appear on any page that uses the master page.
一个master page页面看起来像其他的普通的ASP.NET页面,除了扩展名由.master 来替代了.aspx和一些特殊的控件和标题字段。Master pages必须包含一个和更多的<asp:ContentPlaceHolder>控件。这些控件表示了那些需要替代的内容。基本上那些不在ContentPlaceHolder的内容都将出现在使用master page的页面里了。
Other than these key changes, a master page can contain any HTML or control that can be found on a normal ASP.NET page
除了这些关键地方的改变外,master page页面能包含一个普通的ASP.NET页面上能够存在的任意的HTML代码或者是控件。
看到这里,master pages可能和frame很像了吧,其实他们是不一样的,MS这样说他们的区别:
Although master pages and frames serve a similar purpose, master pages offer much more functionality. Unlike frames, using master pages allow you to:
· Bookmark a page and recall all the information on the specific page, not just the default frame page. A master page isn't really a frame. It's a single page that contains collated content from the master page and the content page that builds on the master. Therefore it looks and acts like a single Web page rather than a frame.
· Work in controls and tags rather than HTML. Thanks to Visual Studio, you don't have to worry about opening and closing the frame tags or modifying countless html attributes to ensure that each frame displays in the fashion you want. You can simply create the place holder and modify its properties through Visual Studio.
· Leverage Visual Studio's code creation to visually design the layout, manage the frames, and provide all of the plumbing to link the content pages into the master page. You can add new content without having to worry that the overall HTML layout of the page will be affected.
