18.5. Dynamically Creating Browser-Specific Stylesheets
Problem
You need to vary the look and feel of your application pages depending on the platform (Mac or Windows) that is being used.
Solution
Place 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:
Use the
Platformproperty of theRequest.Browserobject to obtain the browser’s platform.Check the platform string for the presence of the substring, such as “mac”, which indicates whether the browser is running on a Mac platform.
Based on the platform, programmatically create the HTML style element.
Set the
Textproperty of theasp:Literalcontrol in the head section of the.aspxfile to the created HTML style elements.
The .aspx file used for this example is shown in
Example 18-13. The code-behind is shown in Example 18-14 (VB) and Example 18-15 (C#).
Discussion
HTML is not always rendered the same. Different browsers and different platforms render the HTML in a variety of 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access