Author: Fritz Onion related technologies: templates, ASP.NET, Master Pages Difficulty: ★★★ ☆☆ types of readers: ASP.NET Developers
[Guide] This article first introduces the implementation of "template" under traditional web development, including the implementation of ASP and ASP.NET 1.x, and then introduces the implementation of ASP.NET 2.0 on Templates - Master Pages, last Explain the principle of implementation of the master page.
For many years, Web developers have been working hard to "template" their site through a variety of technologies, but there is no one certified technique that can maintain the true generality of standardized appearance throughout the site. The master page is one of the most anticipated features in ASP.NET 2.0, and eventually provides a method based on template design page based on the application.
Template design
Defining a standard appearance of all pages in Web site design is pleasing. This may include public headers, footers, and menus that provide a set of core functions and appearances throughout the site. For dynamic sites generated by techniques such as ASP or ASP.NET, if they integrate these common functions on all pages, it will be useful for some type of page template, which allows all pages to contain their own unique Content, and provide a central location in sites and behavior in terms of appearance and behavior. See Figure 1 for a simple and specific example of the site that can benefit from some type of page template technology.
Figure 1 Simple template site
The specific page has a header on the top, with a footer at the bottom, has a navigation bar on the left, and has a region that is filled with the remaining space for the page special content. Ideally, the header, footer and navigation bar should only need to define once and apply in some way to all pages in the site.
This is exactly the problem that the master page in ASP.NET 2.0 can be easily solved. By defining a master page, and then create a lot of content pages according to it, you can create a site with a single template (master page) to create a site. However, before I explore the details of the masterpiece, I understand how the developers will generate a template site in ASP and ASP.NET 1.x.
Generate templates in traditional ASP
Many sites generated by ASP 3.0 use the concept of templates - typically use the server-side include (SSI) instruction. The SSI directive provides the ability to insert file content in a specific location within the ASP page. A general technique is to use SSI instructions to introduce common elements, such as footer, header, and navigation bar. Figure 2 demonstrates this approach.
Figure 2 The server side in the ASP contains instructions
Although the technology takes a step in the correct direction, it still means that each page is in addition to generating all page content, but also generates surrounding layout elements and overall structures. Another more template feature of many sites is: Specify two universal include files (such as pagestart.asp and pagend.asp), and make these two files not only contain universal navigation bar, headers and pages The feet include the surrounding layout elements and overall page structures. All elements you have added to each page are its unique content.
This method provides many developers needed in centralized templates of the entire site; but it also has several disadvantages. First, the designer for editing a file containing the SSI directive is not supported, so the designer must implement the merged presentation effect to view the full page appearance, just later need to be resembled. Second, the included mechanism is too simple because it is just inserting the contents of a file into another file before the ASP analysis program performs server-side calculations. This means that it is easy to make mistakes when matching the HTML element end tag and correctly selecting the location to avoid errors. At the same time, it is difficult to customize some parts of the included file. In the ASP "Technique2" example downloadable from the MSDN Magazine Web site, you can notice that a defect that you can notice is that the title of each page is intentionally reserved empty. This happens is that the title is only specified in the top-level page_start containing files, and there is nothing ingenious to let the page containing the template specify the content that appears in the title element. Finally, using the included file requires system overhead because they add the total size of the ASP scripting engine cache. Based on the above-mentioned server-side, a simple method containing instructions, the real-general templating mechanism should have the following five functions in mind:
Has a function that enables developers to create independent definitions, and can be reused in multiple pages to be able to define the "shell" page for other pages as a template to change the inheritance template page. Elements, such as changing the title element, can specify the standby page template in the page to declare the standby page template to actually view the presentation version of the page with the page associated
The server-side containing files are used for ASP 3.0 to meet the first conditions described above, and can give some degrees to satisfy the second condition, but the last three functions cannot be provided.
Generate template sites in ASP.NET 1.x
ASP.NET introduces a new, object-based programming model, which is likely to provide an updated, more elegant template mechanism. ASP.NET provides you with a server-side representation of each page by rendering to the complete hierarchical object model of the client element, without having to rely on the relatively low SSI mechanism in ASP 3.0. The function that is properly reflecting the functionality of the SSI file is the user control. User controls provide a declarative approach to create custom controls that can insert their contents and behaviors into any location of other pages, so use these controls as a general method of the reusable part of the definition page is very simple, as shown in Figure 3 Indicated.
Figure 3 can be reused page part of the user control
In addition to acting as simple content placeholders, user controls can disclose complete controls for attributes, methods, and event functions. Unlike the server side containing instructions, you can assign user controls that can affect its features rendered on the page. For example, you can create an attribute on the header control called showbreadcrumbs so that you can selectively open or close the crush function as needed.
However, the user control reuse model has some limitations and disadvantages. First, many developers have discovered that there must be a register directive to each page will be added to each page of the user control, so it is very troublesome. Ironically, many ASP.NET developers have turned to use SSI instructions to import Register instructions into all of them so that only one SSI directive replace multiple code rows. However, more importantly, without any built-in way can use user controls to meet any of the five conditions listed above (except for the first condition because you can create Reusable page part).
ASP.NET has indeed a more rich object model, and many people have established their own template mechanism to overcome the shortcomings of lack of native template mechanisms in ASP.NET 1.x. With a little creative and some smart codes, you can generate a common template mechanism. Most implementations rely on the following facts: You have the opportunity to manipulate the control hierarchy before the control hierarchy of the Page class is sent back to the client. For example, a technique is to create a common, derived from the base class (the base class), the generated control in the generated page hierarchy), dynamically load the user control of the page template, and then insert the extracted control Known location in the user control hierarchy. The complete implementation of this technology is included in the code download information of this article. With this similar technique, it can be very close to all five conditions needed to achieve a real useful template mechanism. Has a user control for defining a reusable page portion. With a page template mechanism, through this mechanism, a single template can be defined and applied to any number of pages in the system. Use this technology to define the "replaceable" element in the template and can specify the standby template for each page. One of the missing features is designer integration - if there is no Visual Studio .NET to help with this template technology, it is really impossible to implement this feature. Another disadvantage with this similar custom template technology is that no built-in components of this technique are supported in the .NET Framework in the .NET FRAMEWORK. This means that the template mechanisms used in each site may be completely different.
Although ASP.NET provides you with extremely flexible and more powerful programming models, it still does not have a built-in mechanism for templating sites. In particular, although certain developers have generated template mechanisms by subtly using control hierarchical replacement and user controls, they still need to complete additional configuration steps, and more importantly, they do not have designer support. At this time, let us walk into the master page in ASP.NET 2.0
Master page
ASP.NET 2.0 The advent of the masterpiece page represents Microsoft provides the first template mechanism that satisfies all the conditions outlined in front. They provide a page template for site level, a mechanism for performing fine granular content replacement, which template should be programmed and declared control, perhaps the most striking thing that they provide integrated designer stand by. From a technical point of view, the implementation of the master page is very similar to the implementation of the self-defined template mechanism I have described, but adds designer support from Visual Studio 2005, and it is now recognized. And supported by visual inheritance to build template sites, which makes you eventually have a complete template solution. ASP.NET 2.0 Memare page implementation contains two conceptual elements: masterpiece and content pages. The master page acts as a template for the content page, and the content page provides the content to fill the part of the "Filling" in the master page. The master page is essentially a standard ASP.NET page, which is different in that it uses the extension. Master and instructions (rather than use). The master page file acts as a template for other pages, so it usually contains top HTML elements, main forms, headers, footers, and more. In the master page, you can add an instance of adding the ContentPlaceHolder control at the hope that the page-specific content is available, as shown in Figure 4.
<% @ Master Language = "C #"%>