Online Book Reader

Home Category

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

By Root 5694 0
attribute indicates the path to the master page. Page-level binding, however, is just one possibility—although it is the most common one.

You can also set the binding between the master and the content at the application or folder level. Application-level binding means that you link all the pages of an application to the same master. You configure this behavior by setting the Master attribute in the element of the principal web.config file:

If the same setting is expressed in a child web.config file—a web.config file stored in a site subdirectory—all ASP.NET pages in the folder are bound to a specified master page.

Note that if you define binding at the application or folder level, all the Web pages in the application (or the folder) must have Content controls mapped to one or more placeholders in the master page. In other words, application-level binding prevents you from having (or later adding) a page to the site that is not configured as a content page. Any classic ASP.NET page in the application (or folder) that contains server controls will throw an exception.

Device-Specific Masters


Like all ASP.NET pages and controls, master pages can detect the capabilities of the underlying browser and adapt their output to the specific device in use. ASP.NET makes choosing a device-specific master easier than ever. If you want to control how certain pages of your site appear on a particular browser, you can build them from a common master and design the master to address the specific features of the browser. In other words, you can create multiple versions of the same master, each targeting a different type of browser.

How do you associate a particular version of the master and a particular browser? In the content page, you define multiple bindings using the same MasterPageFile attribute, but you prefix it with the identifier of the device. For example, suppose you want to provide ad hoc support for Microsoft Internet Explorer and Firefox browsers and use a generic master for any other browsers that users employ to visit the site. You use the following syntax:

<%@ Page masterpagefile="Site.master"

ie:masterpagefile="ieSite.master"

firefox:masterpagefile="ffSite.master" %>

The ieSite.master file will be used for Internet Explorer; the ffSite.master, on the other hand, will be used if the browser is Firefox. In any other case, a device-independent master (site.master) will be used. When the page runs, the ASP.NET runtime automatically determines which browser or device the user is using and selects the corresponding master page, as shown in Figure 8-4.

Figure 8-4. Browser-specific master pages.

The prefixes you can use to indicate a particular type of browser are those defined in the ASP.NET configuration files for browsers. Table 8-2 lists the most commonly used IDs.

Table 8-2. ID of Most Common Browsers

Browser ID

Browser Name

IE

Any version of Internet Explorer

Netscape3

Netscape Navigator 3.x

Netscape4

Netscape Communicator 4.x

Netscape6to9

Any version of Netscape higher than 6.0

Firefox

Firefox

Opera

Opera

Up

Openwave-powered devices

Blackberry

BlackBerry browser

iPhone

iPhone browser

Chrome

Google Chrome

ieMobile

Internet Explorer for mobile devices

Obviously, you can distinguish not just between up-level and down-level browsers, but you can also distinguish between browsers and other devices, such as cellular phones and personal digital assistants (PDAs). If you use device-specific masters, you must also indicate a device-independent master.

Note

Browser information is stored in text files with a .browser extension located in the Browsers folder under the ASP.NET installation path on the Web server. It’s the same folder that contains machine.config and WINDOWS%\Microsoft.NET\Framework\[version]\Config\Browsers.

Processing Master and Content Pages


The use of master pages slightly changes how pages are processed and compiled. For one thing,

Return Main Page Previous Page Next Page

®Online Book Reader