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 pages region controls (based on their ids).
此控件有两个意图:
·作为一个定位标志,标识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结构后只需要改变一个文件或几个文件就可以实现。
好处不少,当然缺点也不可能少。
有什么其它不明之处,我们下回分解。
