15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Themes ❘ 1207<br />

applying Themes to Pages<br />

You can apply a theme to a page in several ways, declaratively or programmatically. The simplest declarative<br />

way to apply a theme is via the directive, using the Theme or StyleSheetTheme attribute:<br />

or:<br />

<br />

<br />

Here myTheme is the name defined for the theme.<br />

Alternatively, you can specify a theme to use for all pages in a site, using an entry in the web.config file for<br />

your web site:<br />

<br />

<br />

<br />

<br />

<br />

Again, you can use Theme or StyleSheetTheme here. You can also be more specific by using <br />

elements to override this setting for individual pages or directories, in the same way as this element was used<br />

in the previous section for security information.<br />

Programmatically, you can apply themes in the code-behind file for a page. There is only one place where<br />

you are allowed to do this — in the Page_PreInit() event h<strong>and</strong>ler, which is triggered very early on in<br />

the lifecycle of the page. In this event, you simply have to set the Page.Theme or Page.StyleSheetTheme<br />

property to the name of the theme you want to apply, for example:<br />

protected override void OnPreInit(EventArgs e)<br />

{<br />

Page.Theme = "myTheme";<br />

}<br />

Because you are using code to do this, you can dynamically apply a theme file from a selection of themes.<br />

This technique is used in the “Themes in PCSDemoSite” section.<br />

defining Themes<br />

Themes are defined in yet another of the “special” directories in ASP.<strong>NET</strong> — in this case App_Themes. The<br />

App_Themes directory can contain any number of subdirectories, one per theme, where the name of the<br />

subdirectory defines the name of the theme.<br />

Defining a theme involves putting the required files for the theme in the theme subdirectory. For CSS style<br />

sheets, you don’t have to worry about the filename; the theme system simply looks for a file with a .css<br />

extension. Similarly, .skin files can have any filename, although it is recommended that you use multiple<br />

.skin files, one for each control type you want to skin, <strong>and</strong> each named after the control it skins.<br />

Skin files contain server control definitions in exactly the same format as you would use in st<strong>and</strong>ard<br />

ASP.<strong>NET</strong> pages. The difference is that the controls in skin files are never added to your page; they are<br />

simply used to extract properties. A definition for a button skin, typically placed in a file called Button<br />

.skin, might be as follows:<br />

<br />

This skin is actually taken from the DefaultTheme theme in PCSDemoSite, <strong>and</strong> is responsible for the look<br />

of the button on the Meeting Room Booker page you saw earlier in this chapter.<br />

When you create a skin for a control type in this way you don’t use an ID property.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!