Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [174]

By Root 5544 0
a page based on a master has a double dependency—on the .aspx source file (the content page) and on the .master file (the master page). If either of these pages changes, the dynamic page assembly will be re-created. Although the URL that users need is the URL of the content page, the page served to the browser results from the master page being fleshed out with any replacement information provided by the content page.

Compiling Master Pages


When the user requests an .aspx resource mapped to a content page—that is, a page that references a master—the ASP.NET runtime begins its job by tracking the dependency between the source .aspx file and its master. This information is persisted in a local file created in the ASP.NET temporary files folder. Next, the runtime parses the master page source code and creates a Visual Basic or C# class, depending on the language set in the master page. The class inherits MasterPage, or the master’s code file, and is then compiled to an assembly.

If multiple .master files are found in the same directory, they are all processed at the same time. Thus a dynamic assembly is generated for any master files found, even if only one of them is used by the ASP.NET page whose request triggered the compilation process. Therefore, don’t leave unused master files in your Web space—they will be compiled anyway. Also note that the compilation tax is paid only the first time a content page is accessed within the application. When a user accesses another page that requires the second master, the response is faster because the previously compiled master is cached.

Serving the Page to Users


As mentioned, any ASP.NET page bound to a master page must have a certain structure—no server controls or literal text are allowed outside the tag. As a result, the layout of the page looks like a plain collection of content elements, each bound to a particular placeholder in the master. The connection is established through the ID property. The element works like a control container, much like the Panel control of ASP.NET or the HTML

tag. All the markup text is compiled to a template and associated with the corresponding placeholder property on the master class.

The master page is a special kind of user control with some templated regions. It’s not coincidental, in fact, that the MasterPage class inherits from the UserControl class. After it is instantiated as a user control, the master page is completed with templates generated from the markup defined in the content page. Next, the resulting control is added to the control tree of the current page. No other controls are present in the final page except those brought in by the master. Figure 8-5 shows the skeleton of the final page served to the user.

Figure 8-5. The structure of the final page in which the master page and the content page are merged.

Nested Master Pages


So far, we’ve seen a pretty simple relationship between a master page and a collection of content pages. However, the topology of the relationship can be made as complex and sophisticated as needed. A master can, in fact, be associated with another master and form a hierarchical, nested structure. When nested masters are used, any child master is seen and implemented as a plain content page in which extra ContentPlaceHolder controls are defined for an extra level of content pages. Put another way, a child master is a kind of content page that contains a combination of and elements. Like any other content page, a child master points to a master page and provides content blocks for its parent’s placeholders. At the same time, it makes available new placeholders for its child pages.

Note

There’s no architectural limitation on the number of nesting levels you can implement in your Web sites. Performance-wise, the depth of the nesting has a negligible impact on the overall functionality and scalability of the solution. The final page served to the user is always compiled on demand and never modified as long as

®Online Book Reader