简体   繁体   English

就OOP而言,内容页面和母版页面之间有什么关系?

[英]What is the relation between a content page and master page in terms of OOP?

What are class relations? 什么是阶级关系? Can anybody help to explain the relationship between a content page and a master page in terms of OOP? 有人可以根据OOP来解释内容页面和母版页面之间的关系吗?

I guess that you are really asking what is the relationship between a page and its masterpage from a programming perspective? 我想您真的是在从编程角度询问页面与其主页之间的关系吗? My answer is that the relationship is not what most people would assume . 我的回答是, 这种关系不是大多数人会假设的 You might think that a MasterPage contains the page, because looking at the html markup and the ContentPlaceHolders, the MasterPage html elements do end up containing the html elements on the page. 您可能会认为MasterPage包含该页面,因为查看html标记和ContentPlaceHolders,MasterPage html元素的确包含页面上的html元素。

In fact the relationship is the other way round. 实际上,这种关系是相反的。 The page owns the MasterPage. 该页面拥有MasterPage。 The PreInit method on the page allows you to change the MasterPage. 页面上的PreInit方法允许您更改母版页。

The best way I can describe what comes next is that the Page wraps itself in the MasterPage . 我可以描述下一步的最佳方法是Page将其自身包装在MasterPage中 After the content page's PreInit event, but before its Init event, the MasterPage content is inserted in and around the asp:Content blocks, according to the position of the ContentPlaceHolders on the MasterPage. 在内容页面的PreInit事件之后,但在其Init事件之前,根据ContentPlaceHolders在MasterPage上的位置,将MasterPage内容插入到asp:Content块中及其周围。

Generally, event handlers on the page execute before analogous ones on the MasterPage, which is treated much like a Control that has been injected in and around the Page. 通常,页面上的事件处理程序先于MasterPage上的类似事件处理程序执行,该事件处理程序被视为已插入到Page中及其周围的控件。

You can see this in this blog entry by Tim Gaunt 您可以在Tim Gaunt的博客条目中看到此内容

Extract: 提取:

Page     Start OnLoad(EventArgs e)
Page     Page_Load(object sender, EventArgs e)
Page     End OnLoad(EventArgs e)
MasterPage     Start OnLoad(EventArgs e)
MasterPage     Page_Load(object sender, EventArgs e)
MasterPage     End OnLoad(EventArgs e)
UserControl     Start OnLoad(EventArgs e)
UserControl     Page_Load(object sender, EventArgs e)
UserControl     End OnLoad(EventArgs e)
CustomWebControl     Start OnLoad(EventArgs e)
CustomWebControl     End OnLoad(EventArgs e)

Your "content" page inherits from a "base content page" class. 您的“内容”页面继承自“基本内容页面”类。 Your "MasterPage" inherits from a "base master page" class. 您的“ MasterPage”继承自“基本母版页”类。 In your content class you define the relationship between your content page to the master page. 在内容类中,定义内容页面与母版页面之间的关系。 The base classes handle the main plumbing between the two. 基类处理两者之间的主要管道。

A master page inherits from Sytem.Web.UI.MasterPage, while a page inherits from System.Web.UI.Page 母版页继承自Sytem.Web.UI.MasterPage,而页面则继承自System.Web.UI.Page

But, think of a MasterPage as a type of control. 但是,可以将MasterPage视为一种控件。

If your question relates to allowing one to talk to the other, then there are numerous techniques to accomplish this. 如果您的问题涉及允许一个人与另一个人交谈,那么有很多技术可以做到这一点。

Use of the directive <%@ MasterType VirtualPath="~/templates/Base.master" %> or <%@ MasterType TypeName="SomeNamespace.SomeMasterPageBaseClass" %> can set up a strong typing between your content page and master page. 使用指令<%@ MasterType VirtualPath="~/templates/Base.master" %><%@ MasterType TypeName="SomeNamespace.SomeMasterPageBaseClass" %>可以在内容页面和母版页之间建立强类型。

If you're dealing with nested master pages, you can also make use of <%@ Reference VirtualPath="~/templates/base.master" %> 如果要处理嵌套的母版页,则还可以使用<%@ Reference VirtualPath="~/templates/base.master" %>

A good description of complex interactions is here: 复杂交互的良好描述在这里:

http://www.odetocode.com/articles/450.aspx http://www.odetocode.com/articles/450.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM