Quick site standardization using master pages

Source: Internet
Author: User

Paul Wilson

Microsoft most valuable expert in ASP. NET

October 2003

Applicable:

Microsoft ASP. NET Whidbey

Microsoft Visual Basic. net

Abstract:Understand the features of the master page, which is coming soon. A flexible Page Template system in the. Net version provides excellent visual designer support, codenamed "Whidbey" (based on the upcoming Microsoft Visual Studio. net release version ). (10 pages)

Download the ASP. NET pro sample.

Content

Content on this page
Multi-region and header flag
Nested and Dynamic Master
Advantages of the master page

NoteThis document has been written before the corresponding software products are officially released. Therefore, we cannot guarantee that any details involved in this document are exactly the same as those of the final product. The description in this article is for the products published in this article and is only for reference during planning. No further notice will be given if there are any changes.

All pages of most web sites share a layout, but the traditional ASP and ASP. NET do not include automatic support for page templates. In addition, although traditional ASP usually uses include files, Asp. the user controls and basic page classes are often used in. net, but these methods are not built-in and are not supported by the designer. Microsoft ASP. Net Whidbey changes this situation by adding a flexible Page Template system named "master page. This template system should be simple enough for designers to use, and its functions should be powerful enough for developers to use.

The simplest way to learn this new master PAGE method is to examine some examples. Therefore, the rest of this article will show you several examples, starting from the simplest situation, gradually expand to more advanced scenarios.

The first example creates a master with the site title, left and right sidebar, site footer, and a single content "region" to illustrate various new concepts and syntaxes on the master page. First, create a master page as a public layout. The master page is very similar to the user control, except that it has a new master file extension and a master command. Unlike most user controls, a master page contains top-level HTML tags, such as <HTML>,

Code example 1. The introlayout. master file is a master page that defines the public layout of other pages. This includes the title, left and right sidebar, footer, and single content area.

<%@ master language="VB" %> 

Finally, create a content page for the content specific to the page, where the page command uses the newMasterProperty to link to the master page. In addition to an optional server script block, the only top-level element that can be used is the new content control, which replaces the matched contentplaceholder control (defined on the master page ). The content control has a contentplaceholderid attribute, which must match the original specified ID of the contentplaceholder control to be overwritten. Otherwise, the content of the page in the content area includes common HTML tags and server controls, including page or control event handlers. Note thatMasterAttribute <pages> element. You can specify a default master in the web. config file, but this function is not currently supported by the designer.

Code Example 2. This introcontent. aspx file is the content page that uses the introlayout. master page. It only contains the page-specific content that corresponds to the region defined in the master page.

<%@ page language="VB" master="~/IntroLayout.master" %> <script runat="server" language="vb"> Sub Submit_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Result.Text = TestValue.Text End Sub </script> <asp:content id="Main" runat="server" contentplaceholderid="Main"> <div align="center"> 

Of course, we can only use the SDK to compile this example in notepad, but when using Microsoft Visual Studio like "Whidbey. net IDE, the value of the master page is truly reflected. Figure 1 shows the design-Time View of the content page when this master page is used to display how the entire page is visually displayed during design. The master page is displayed, but the master page is gray, because it is not editable, And the content page is completely designed, it is surrounded by a brand new design task panel. Unlike all Page Template methods used in earlier versions of Visual Studio. NET, Microsoft intelliisense, drag-and-drop, or any other design features are available here.


Figure 1. Visual Studio. Net of Whidbey includes support for master page design. The master page is displayed in gray, and the specific content on the page is editable.


Multi-region and header flag

The following example shows how to use multiple Content regions in the master page and how to extend the master page by using custom public attributes. First, change the right sidebar to a contentplaceholder control. In this case, it is ID right, so that the region can be replaced with a content control in each page linked to the master. This new area will be displayed in Visual Studio.. Net designer and can be fully editable, just like other content areas. If this area is not covered in the actual page, the default content is displayed. Next, change the <title> label to a part of the contentplaceholder control, so that you can easily set the title, style sheet link, and sheet Ag on each actual page. Please note that this content area will not be displayed in the designer because it is located in the head instead of the body, but just like the normal head mark, you can easily edit HTML source files. Next, create a public string attribute named leftsidebar (or create a field for simplicity) and create a literal control for the left sidebar, set the text property of the control to the value in this property master prerender event. In this simple example, another content area can be created, but it is important to know that for more complex situations, the custom public attributes on the master page are easy to create and use.

Code example 3. This multipleregions. master file has multiple regions, one of which is used for the title and other head tags. A custom public attribute is also defined, which can be conveniently set on each actual page.

<%@ master language="VB" %> <script runat="server" language="vb"> Public LeftSideBar As String _ = "Master.LeftSideBar Goes Here" Sub Page_PreRender(ByVal sender As Object, _ ByVal e As System.EventArgs) Left.Text = LeftSideBar End Sub </script> 

Finally, on the change content page, use the Matching content control to overwrite the new region, or if you want to keep the default content defined for each region, do not change it. Remember, for areas used for HTML head content (including titles, style sheet links, and sheet AG), you must manually edit them in the HTML source file because the designer only displays the body area. Finally, although you may need to save or reload the master, you also need to set the custom public attribute value of the master page in the load event (this value can be used even in intelliisense ).

Code example 4. The multipleregions. aspx file is a content page that uses the multipleregions. master page. It includes multiple regions (including one area in the HTML head) and uses a custom attribute.

<%@ page language="VB" master="~/MultipleRegions.master" %> <script runat="server" language="vb"> Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Master.LeftSideBar = "Page-Specific Left SideBar" End Sub Sub Submit_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Result.Text = TestValue.Text End Sub </script> <asp:content id="Head" runat="server" contentplaceholderid="Head"> <title>Master Pages: Multiple and Head Regions</title> </asp:content> <asp:content id="Main" runat="server" contentplaceholderid="Main"> <div align="center"> Back to Top


Nested and Dynamic Master

The last example shows how to use nested master pages and how to enable dynamic master pages based on user input at runtime. Note that, at least for this test version, the designer does not support nested masters and does not officially recommend the methods required to implement dynamic masters in this test version, this is because we should provide better formal support methods in subsequent versions. First, create a parent master with only the site title, site footer, a single sub-content area, and an HTML head area.

Next, create a sub-level master to replace the sub-area. The left sidebar of the sub-level master is public as a custom attribute, and the right sidebar is public as a content area, the central region is the primary content area. In addition, you must overwrite and replace the HTML head area with another content area to make the area public to the actual page, this is required at least for the current test version (see the included code download ).

Finally, change the content page to link to this sub-level master and overwrite the content area as described above. Next, add a "device filter" master in the instructions on the page. In this case, the master will be named "alternate ". Then, override the testdevicefilter method and return true if the alternate master's conditions are met. In this case, the alternate master is used when there is a sending back, although the form set must be used to determine this situation (because ispostback is not available in the early stages of the page lifecycle ). Finally, set the custom public property value of the master page in the load event, although this will require forced conversion of the master type (because the master is currently dynamic ). Note that this method is not officially recommended, but this is the only way to implement dynamic master in the test version, this approach is still under discussion and is likely to be changed before the final release to better support dynamic masters.

Code example 5. The nestedlayouts. aspx file is a content page that uses the nested childlayout. master page. When the page is a sending back, multipleregions. Master is also dynamically used.

<%@ page language="VB" master="~/ChildLayout.master" alternate:master="~/MultipleRegions.master" %> <script runat="server" language="vb"> Overrides Function TestDeviceFilter( _ ByVal deviceFilterName As String) As Boolean If deviceFilterName.Equals("alternate") Then If Request.Form.Count > 0 Then Return True End If End If Return MyBase.TestDeviceFilter(deviceFilterName) End Function Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) If TypeOf Master Is ASP.ChildLayout_master Then CType(Master, ASP.ChildLayout_master) _ .LeftSideBar = "Nested Layouts Left SideBar" ElseIf TypeOf Master Is ASP.MultipleRegions_master Then CType(Master, ASP.MultipleRegions_master) _ .LeftSideBar = "Multiple Regions Left SideBar" End If End Sub Sub Submit_Click(ByVal sender As Object, _ ByVal e As System.EventArgs) Result.Text = TestValue.Text End Sub </script> <asp:content id="Head" runat="server" contentplaceholderid="Head"> <title>Master Pages: Nested and Dynamic Masters</title> </asp:content> <asp:content id="Main" runat="server" contentplaceholderid="Main"> <div align="center"> Back to Top


Advantages of the master page

The master page system is easy for designers to use because it is based on the familiar User Control Model of ASP. NET. Although almost complete visualization is added, no code is required. On the other hand, master pages are powerful because they support multi-region, default content, nested templates, and device filters (for browser dependencies ). The master page is fully compiled to provide the best performance. It also provides a strong programming model (including the intelliisense during design of the master attribute ), although some trade-offs may be made before the final release to better support dynamic masters.

Of course, the official release version of ASP. NET Whidbey has not yet been released. However, there are several versions of master pages that can run under versions 1.0 and 1.1, although these versions are naturally not integrated into the Visual Studio designer. For more information, see ASP. on the web site of the net alliance, Paul Wilson's ASP. net corner: masterpages: introduction is an initial example of Microsoft, while masterpages: improved version provides a custom version that does not break the existing designer. The Code download provided in this article also contains almost identical examples as in this article, where the master page of my custom version is used in versions 1.0 and 1.1.

About the author

Paul WilsonHe is a software architect in Atlanta and recently joined the PRG-Schultz development team. Its wilsonwebform control allows multiple forms and non-return forms to be used in ASP. NET. This is ASP. net, Microsoft's most valuable expert, Microsoft's ASP.. Net forum administrator, Microsoft certified solution developers, Microsoft certified application developers, Microsoft certified database administrators, and Microsoft Certified System Engineers. If you want to contact him, visit his Web site, www.wilsondotnet.com, or send an email to the Paul@WilsonDotNet.com.

This article was originally published in ASP. netpro magazine December 2003. The publisher authorizes re-printing.

Transfer to original English page

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.