[收藏]Implementing Master Pages in ASP.NET

 

Implementing Master Pages in ASP.NET

 

Download Master Pages Control and Sample Code:http://www.asp.net/ControlGallery/default.aspx?Category=40&tabindex=2
This sample demonstrates how to implement master pages in ASP.NET.  This is sometimes referred to as Visual Inheritance, Page Inheritance, or Page Templating.

What are master pages?

A lot of sites want to be able to design the layout of their pages in a single place (the Master Page), and then have all the other pages use it, without needing to duplicate any HTML.  For example, a news site would want to have the same header and footer in all their pages, while the actual content of the article changes.  While ASP.NET doesn't directly have a feature to handle this scenario, it is quite easy to achieve the required behavior using a User Control.

What is included in this sample

The most important file is MasterPages.cs, which implements the basic framework that makes Master Pages work.  You should be able to use this code as is (though you can certainly modify it as you wish).  Use mk.bat to build MasterPages.cs.  An example that uses the framework is also included.  It is made up of the files TestMasterPage.ascx and TestPage.aspx.  You can use that as a starting point for your own pages.

How to use this framework

  • The master layout is defined in a User Control (ascx file).  Note that this is quite a departure from the way User Controls are typically used.  In most cases, a User Control renders within a page.  But in this case, we reverse the situation by having the User Control define the Master Page.  The only thing that differentiates a Master Page User Control from a regular one is that it uses one or more tags to define where the per-page content needs to be inserted.  Those tags can have some default content, which is used if the aspx page which uses the ascx master page doesn't define the matching region.
  • Then, the per-page content is defined in Pages (aspx files).  Those Pages need to do a few simple things to use the Master Page:
    • they define a top level tag, with a MasterPageFile attribute pointing to the master page (ascx) file.
    • the tag contains one or more tags to define the customizable content of the Master Page.  Those tags should have an ID that matches the ID of the tag for which they are defining content.
    • the tag should not include any content outside of the tags.  If they do, an error will occur.

      Instructions to build and run the sample

      • Unzip all the files into the root directory of a web application with a bin directory
      • Run mk.bat to build MasterPages.dll into your bin directory
      • Use your browser to request the test page http://localhost/yourtestapp/TestPage.aspx

      Please send feedback and comments to davidebb@microsoft.com

       

    • for which they are defining content.
posted @ 2004-02-22 22:28 dudu 阅读(3799) 评论(4)  编辑 收藏 所属分类: ASP.NET

  回复  引用    
#1楼 2004-12-06 15:32 | dd [未注册用户]
hao
  回复  引用    
#2楼 2005-05-06 17:23 | 笑容 [未注册用户]
在 ASP.Net中实现Master Pages(模板)
注:阅读和理解一份英文资料就是一个翻译的过程。只要把你的思想记录下来,那么就是一个不错的译文。
笑容原创,转载著名来自:http://www.oo8h.com/news_375.html

下载Master Pages控件和实例程序

地址 http://www.asp.net/ControlGallery/default.aspx?Category=40&tabindex=2

这个例子示范了在Asp.Net如何实现Master Pages。这是一份虚拟继承、Page继承、或者是Page模板(Template)的参考资料。

什么是Master Pages ?

大多网站都会设计一个计划页面,也就是我们常说的模板页面(The Master Page)。所有页面都使用了这个页面的风格(或直接使用这个模板),不需要使用很多个重复相同的Html页面。

例如说,每个页面都会有相同的页眉(Head 或者Nav 导航条)、页脚(Footer,联系我们之类的相同的底部),只有每个页的实际内容会发生改变。Asp.Net 没有直观的对于这种问题的解决方案,但是,我们可以通过使用User Control 来轻松快速的实现这个目的。

总结:简单点说,MasterPage是用来简单的实现模板技术的一个UserControl(用户自定义控件)。

这个例子里都包含了些什么?

最重要的文件是 MasterPages.cs 它是最基本的PasterPage的框架,是它使MasterPage这个技术可以工作的。你可以直接使用这个文件,也可以根据你的需要去改编成更适合自己需要的版本。代码包里包含了mk.bat是用来编译MasterPages.cs的,一个使用MasterPage的例子被包含在这里面。TestMasterPage.ascx 和 TestPage.aspx 就是这个实际使用的例子,这个例子将成为把MasterPage技术使用到你的程序里的一个起点。



如何使用这个框架

主要的版面规划定义在一个用户控件中(User Control ,ascx file)。这是一个违背了正常UserControl的使用规则的做法。大多数时候一个UserControl是被渲染和显示在一个页面中的,但是在这里,完全是不一样的。
每个页面都要做的一些事情是准备自己要显示的内容,然后做一些每个页都要做的相同的事情就是:
定义顶级标志(Tag)使用MasterPageFile属性,定义其MasterPage (ascx页)文件。
这个标记包含了一个或多个自定义内容(MasterPage)通过不同的ID来进行标志。
这些标记不能访问父一级和以上的标记(Tag、MasterPage),如果你这么做,将会有错误发生。
指导如何建立和运行这个例子

解压缩文件到root目录,把程序文件复制到bin目录。
运行mk.bat编译这些masterpage.dll到bin目录
使用浏览器访问地址 类似于 http://localhost/yourtestapp/TestPage.aspx
有什么问题和反馈可以发送给 davidebb@microsoft.com (不提供中文支持)

如果发现翻译有错误,欢迎指正,留言给笑容:http://www.oo8h.com/express.php



原文翻译自:http://www.cnblogs.com/dudu/articles/1503.aspx



英文不太好,一边看词霸一边翻译了一下我的理解,有什么不对的地方,还请指出来。我会及时修正的。谢谢。
  回复  引用  查看    
#3楼 [楼主]2005-05-06 19:13 | dudu      
@笑容
翻译的不错!
这篇文章是我收藏的文章, 原文现在已经找不到了。
  回复  引用  查看    
#4楼 2006-07-08 15:14 | Nina      
@dudu
請問什麼單獨一個頁面的時候,可以運行檢查空值和數字驗證的js。但是一旦套用了master page就不運行了?
詳情請見http://nina-piaoye.cnblogs.com/archive/2006/07/08/445794.html

标题  
姓名  
主页
Email (只有博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2005-05-06 18:07 编辑过
 
另存  打印