asp.net2.0 Master Pages use

Source: Internet
Author: User

asp.net 2.0-master pages:tips, Tricks, and traps

Posted by Scott on April 11, 2006

MasterPages are a great addition to the ASP.net 2.0 feature set, but are not without their. This article would highlight the common problems developers face with master pages, and provide tips and tricks to use mast Er pages to their fullest potential.

Master pages are a great addition to the ASP.net 2.0 feature set. Master pages help us build consistent and maintainable user interfaces. Master pages, however, are not without their quirks. Sometimes master page behavior is surprising, and indeed the very name Master page can be a bit misleading. In this article, we are going to examine some of the common problems-run into as using master pages, and demo Nstrate some practical advice for making effective the use of master pages. For a introduction to master pages, click "Master Pages in asp.net 2.0".

To make use of master pages, we have need to understand how Master pages work. Many of the tips and traps covered later in this article revolve around understanding the Magic Master pages. Let's dig into these implementation details.

For Internal

When a Web request arrives for a asp.net Web Form using a master page, the content page (. aspx) and master page (. master) Merge their content together to produce a single page. Let ' s say we are using the following, simple master page.

<%@ Master language= "VB"%>

<title>untitled page</title>
<body>
<form id= "Form1" runat= "Server" >
<div>
<asp:contentplaceholder id= "ContentPlaceHolder1" runat= "Server" >
</asp:ContentPlaceHolder>
</div>
</form>
</body>

The master page contains some common elements, like a head tag. The most important server-side controls are the form tag (Form1) and the ContentPlaceHolder (ContentPlaceHolder1). Let's also write a simple Web form to use our master page.

<%@ Page language= "C #" masterpagefile= "~/master1.master"
Autoeventwireup= "true" title= "Untitled Page"%>
<asp:content id= "Content1" runat= "Server"
Contentplaceholderid= "ContentPlaceHolder1" >
<asp:label id= "Label1" runat= "Server" text= "Hello, World"/>
</asp:Content>

The Web form contains a single content control[contents Control ], which in turn is the proud parent of a La Bel. We can visualize what the object hierarchies would look like in runtime with the following diagram.

At this point, the page and master page are two separate objects, each with their own. When it comes the "master page" of "the", the master page replaces the page's children with itself.

The master page ' s next step are to look for Content controls in the controls formerly associated with the page. When the master page finds a Content control that matches a contentplaceholder, it moves the controls into the matching Co Ntentplaceholder. In my simple setup, the master page would find a match for ContentPlaceHolder1, and copy over the Label.

All of this work occurs after the content page ' s PreInit event, but before the content page ' s Init event. During This brief slice of time, the master page is deserving to its name. The master page is in control-giving orders and rearranging controls. However, by the time of the Init event fires the master page becomes just another child control inside the page. In fact, the MasterPage class derives from the UserControl class.

I ' ve found it useful to only the master pages as masters during design time. When the application is executing, it's better to the "master page as just another child control."

[I found the following idea to be more helpful, which is to think of master pages as a master at design time, and when the application runs, think of it as just another child control]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.