ProblemYou need to vary the look and feel of your application pages depending on the platform (Mac or Windows) being used. SolutionPlace an asp:Literal control in the head section of the .aspx file, and then set the text property of the control to an HTML style element created programmatically in the code-behind. Use the properties of the Request.Browser object to determine the platform type and control the generation of the style element. In the .aspx file, place an asp:Literal control in the head section. In the code-behind class for the page, use the .NET language of your choice to:
The .aspx file used for this example is shown in Example 21-13. The code-behind is shown in Examples 21-14 (VB) and 21-15 (C#). DiscussionHTML is not always rendered the same. Different browsers and platforms render the HTML in various ways. This sometimes requires using a different stylesheet as a function of the browser or platform the browser is running on (Windows, Mac, etc.) to get the same visual effect. Refer to the Platform property of the HttpBrowserCapabilities class in the MSDN Library for a complete list of platforms detected. For instance, the displayed size of a font of a given point size is larger on the Windows platform than on the Mac platform. This is caused by the difference in screen resolution. The Mac platform uses a display resolution of 72 dots/inch (DPI), resulting in 1 point being equivalent to 1 pixel. The Windows platform uses a display resolution of 96 DPI, resulting in 1 point being equivalent to 1 1/3 pixels. To display a font the same size on both platforms, the point size must change as a function of the platform. Our example programmatically generates a different stylesheet depending on whether the browser is running on a Windows or a Mac platform. It creates a stylesheet in the HTML that has four classes defined for a small, regular, large, and extra-large font. This technique is not new to ASP.NET, but the method of generating the stylesheet dynamically is much easier. Figure 21-3 shows the output of our example on a PC platform. Figure 21-4 shows the output on a Mac platform without generating a stylesheet specific to the Mac (the fonts are smaller than the ones in Figure 21-3). Figure 21-5 shows the output on a Mac platform using a specific stylesheet (the fonts are the same size as the ones in Figure 21-3). Figure 21-3. Example program output for PC platformFigure 21-4. Example program output for Mac platform without specific stylesheetFigure 21-5. Example program output for Mac platform with specific stylesheetServer controls can be placed almost anywhere in an .aspx file and are not restricted to the body section of the page. In our example, an asp:Literal control is placed in the head section of the .aspx file to provide a mechanism to output a stylesheet in the HTML sent to the browser. <head> <title>Dynamically Generating Stylesheet</title> <link rel="Stylesheet" href="css/ASPNetCookbook.css" /> <asp:Literal runat="server" /> </head>
In the Page_Load method of the code-behind, the platform the browser is running on is obtained from the Platform property of the Request.Browser object. The platform name is converted to lowercase to simplify the check for the specific platform. The platform string is checked for the presence of the substring mac, which indicates the browser is running on a Mac platform. After determining the platform, four variables are set to indicate the point size to use for the small, regular, large, and extra-large fonts. Next, a StringBuilder is used to create the HTML style element. The style element includes a class for smallFont, regFont, largeFont, and xLargeFont. Each class contains a font-family style along with a font-size style. The font size is set using the variables described earlier. Finally, the Text property of the asp:Literal control placed in the head section of the .aspx file is set to the style element. Here is the resulting style element rendered in the HTML for the Windows platform: <style type='text/css'> .smallFont {font-family: Verdana, Arial, Helvetica, sans-serif; font-size:8pt;} .regFont {font-family: Verdana, Arial, Helvetica, sans-serif; font-size:10pt;} .largeFont {font-family: Verdana, Arial, Helvetica, sans-serif; font-size:14pt;} .xLargeFont {font-family: Verdana, Arial, Helvetica, sans-serif; font-size:18pt;} </style> Here is the resulting style element rendered in HTML for the Mac platform: <style type='text/css'> .smallFont {font-family: Verdana, Arial, Helvetica, sans-serif; font-size:11pt;} .regFont {font-family: Verdana, Arial, Helvetica, sans-serif; font-size:13pt;} .largeFont {font-family: Verdana, Arial, Helvetica, sans-serif; font-size:19pt;} .xLargeFont {font-family: Verdana, Arial, Helvetica, sans-serif; font-size:24pt;} </style> The classes in the stylesheet can be used like any other class that is hardcoded in the HTML or provided in a cascading stylesheet (CSS): <table width="90%" align="center" border="0"> <tr> <td align="center" > This is the small font. </td> </tr> <tr> <td align="center" > This is the regular font. </td> </tr> <tr> <td align="center" > This is the large font. </td> </tr> <tr> <td align="center" > This is the extra large font. </td> </tr> </table> The solution provided in this recipe can be placed in a user control, giving you the ability to reuse the code in all pages of your application. Refer to Chapter 5 for examples of user controls. An alternate solution to generating the stylesheet programmatically would be to place a link element in the head section and set the href attribute to a different prebuilt cascading stylesheet as a function of the browser and/or platform. This approach will yield better performance because the stylesheet would not be built for each page request. To implement the alternate solution, place the following link element in the head section of the .aspx file. Add the / at the end to close the element or an exception will be thrown when ASP.NET parses the page. <link runat="server" rel="stylesheet" /> In the Page_Load method of the code-behind, add the href attribute to the linkCSS control setting the value to the required cascading stylesheet. The href attribute must be added using the Add method of the Attributes collection because the generic HTML server control does not have an href property. check the users platform platform = Request.Browser.Platform.ToLower() 'set font sizes as a function of the platform If (platform.IndexOf("mac") > -1) Then 'platform is a Mac so add Mac CSS linkCSS.Attributes.Add("href", _ "css/Mac.css") Else 'since not a Mac, assume Windows and add Windows CSS '(production app may want to do additional checks if 'required for styles) linkCSS.Attributes.Add("href", _ "css/Windows.css") End If string platform = null; // check the users platform platform = Request.Browser.Platform.ToLower(); // set font sizes as a function of the platform if (platform.IndexOf("mac") > -1) { // platform is a Mac so add Mac CSS linkCSS.Attributes.Add("href", "css/Mac.css"); } else { // since not a Mac, assume Windows and add Windows CSS // (production app may want to do additional checks if // required for styles) linkCSS.Attributes.Add("href", "css/Windows.css"); } The resulting link element for the Windows platform is shown next. An href attribute has been added to the rendered HTML, with the value set to the required cascading stylesheet: <link rel="stylesheet" href="Windows.css"></link> The code for this alternate example can be placed in a user control to provide easy reuse in all of the pages in your application. Refer to Chapter 5 for examples of user controls. See AlsoChapter 5 for user control examples; MSDN Library for more information on the HttpBrowserCapabilities class and Platform property values Example 21-13. Dynamically generated stylesheet (.aspx)
Example 21-14. Dynamically generated stylesheet code-behind (.vb)
Example 21-15. Dynamically generated stylesheet code-behind (.cs)
|