了解Master Pages库

aspnet forums界面的最关键的问题首先在于它使用了MetaBuilders的Master Pages 控件.
到http://www.metabuilders.com/Tools/MasterPages.aspx处下载此控件来研究一下:

一、Master Pages包括四个类:

(1)Content: This control contains the content for a particular region 
            此类控件包含真实内容

(2)ContentContainer: This control serves two distincts purposes: - it marks the location where the Master Page will be inserted into the Page - it contains the various Content sections that will be matched to the Master Page's Region controls (based on their ID's).
                    此控件有两个意图:
                    ·作为一个定位标志,标识Master Page将被插入到页中;
                    ·与Region Controls相匹配

(3)NoBugForm: A server form that does not cause a problem by being inside a master page or other naming container.
             无错form。可以放心使用

(4)Region: The control marks a place holder for content in a master page 
           占位控件

二、我们通过分析default.aspx来看看Master Page使用方式
(1)default.aspx的内容如下:

< mp:ContentContainer runat = " server "  id = " MPContainer "  MasterPageFile = " ~/Themes/MasterPage.ascx " >  
    
< mp:Content id = " HeadTag "  runat = " server " >  
        
< meta http - equiv = " Refresh "  content = " 300 "   />  
    
</ mp:Content >  
    
< mp:Content id = " MainContent "  runat = " server " >  
        
< Forums:ForumGroupView runat = " server "   />  
    
</ mp:Content >  
</ mp:ContentContainer >  

mp:ContentContainer是一个容器,MasterPageFile="~/Themes/MasterPage.ascx是它最重要的属性,指向了一个ascx控件页。其实,这个ascx控件并不是一个真的ascx控件,而是一个页面框架。它提供了default.aspx页面的主结构,然后在其中留出了空白,让default.aspx来填空。

(2)再来仔细看看masterpage.ascx的内容

< html >  
< head >  
<!-- 标题 -->  
< Forums:PageTitle  runat ="server"   />  
<!-- 风格定义 -->  
< Forums:Style  id ="Style1"  runat ="server"   />  
<!-- 头标签 -->  
< mp:region  id ="HeadTag"  runat ="server"   />  
</ head >  

< body  leftmargin ="0"  topmargin ="0"  marginwidth ="0"  marginheight ="0" >  

<!--  ********* NoBugForm:START ************* // -->  
< mp:NoBugForm  runat ="server" >  

< mp:region  id ="MainContent"  runat ="server" > Default main content </ mp:region >  

</ mp:NoBugForm >  
<!--  ********* NoBugForm:END ************* // -->  

<!--  ********* Footer:START ************* // -->  
< Forums:Footer  runat ="server"   />< br  />  
<!--  ********* Footer:END ************* // -->  
</ body >  
</ html >  

首先,它有html文件的<head>,在<head>中留下了不小的地方放自定义控件,Forums:PageTitle 和Forums:Style
然后,在Body中有一个NoBugForm,这个的作用先猜测一下,可能是指用于mp控件的form。
最为重要的是它有一个
<mp:region id="MainContent" runat="server">Default main content</mp:region>
region是一个占位控件,它给谁占的位置呢?看看default.aspx就明白了:

<mp:Content id="MainContent" runat="server">
   <Forums:ForumGroupView runat="server" />
</mp:Content>

看到了吗?content中的id与region的id相匹配。结果,default.aspx将会在这儿显示出来。
 
三、优点考虑:
现在看来master pages的结构也相对简单。但为什么要使用这种方式而不是直接使用ascx控件呢?
关键在于换肤。
asp forums为了实现换肤的方便而使用了一个专门的ascx文件作为框架页。而通常的ascx文件是不可能作为框架页的,它只能是一个页面中的一部分,我们还需要一个aspx或html文件作为框架页。如果框架页改变了,则所有使用此框架的页面全部都要改过。这在dreamweaver中可以使用模板来实现,但也相当不方便。(我没有用过模板,只知道其是自动更改使用模板的页)但使用期master pages结构后只需要改变一个文件或几个文件就可以实现。


关于asp.net 2.0 的MasterPage,可以参考:
http://www.microsoft.com/taiwan/msdn/library/2004/oct-2004/masterpages.htm

你可能感兴趣的:(master)