开发者

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

开发者 https://www.devze.com 2023-01-16 01:06 出处:网络
What are class relations? Can anybody help to expl开发者_高级运维ain the relationship between a content page and a master page in terms of OOP?I guess that you are really asking what is the relationsh

What are class relations? Can anybody help to expl开发者_高级运维ain the relationship between a content page and a master page in terms of 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.

In fact the relationship is the other way round. The page owns the MasterPage. The PreInit method on the page allows you to change the MasterPage.

The best way I can describe what comes next is that the Page wraps itself in the 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.

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.

You can see this in this blog entry by 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. 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

But, think of a MasterPage as a type of control.

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.

If you're dealing with nested master pages, you can also make use of <%@ Reference VirtualPath="~/templates/base.master" %>

A good description of complex interactions is here:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消