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 ❘ 1209<br />

This sort of theme is useful in, for example, printable versions of web pages. The BareTheme directory<br />

actually consists of no files at all — the only file in use here is the root StyleSheet.css style sheet.<br />

The demo site also contains a third theme, called LuridTheme. This brightly colored <strong>and</strong> difficult-to-read<br />

theme is just a bit of fun, really, but it does show how the look of a site can be dramatically changed<br />

using themes. On a more serious note, themes similar to this can be used to provide high-contrast or largetext<br />

versions of web sites for accessibility purposes.<br />

In PCSDemoSite, the currently selected theme is stored in session state, so the theme is maintained when<br />

you navigate around the site. The code-behind file for /Configuration/Themes/Default.aspx is as<br />

follows:<br />

public partial class _Default : MyPageBase<br />

{<br />

private void ApplyTheme(string themeName)<br />

{<br />

if (Session["SessionTheme"] != null)<br />

{<br />

Session.Remove("SessionTheme");<br />

}<br />

Session.Add("SessionTheme", themeName);<br />

Response.Redirect("~/Configuration/Themes", true);<br />

}<br />

void applyDefaultTheme_Click(object sender, EventArgs e)<br />

{<br />

ApplyTheme("DefaultTheme");<br />

}<br />

void applyBareTheme_Click(object sender, EventArgs e)<br />

{<br />

ApplyTheme("BareTheme");<br />

}<br />

}<br />

void applyLuridTheme_Click(object sender, EventArgs e)<br />

{<br />

ApplyTheme("LuridTheme");<br />

}<br />

code snippet PCSDemoSite/Configuration/Themes/Default.aspx.cs<br />

The key functionality here is in ApplyTheme(), which puts the name of the selected theme into session state,<br />

using the key SessionTheme. It also checks to see if there is already an entry here, <strong>and</strong> if so, removes it.<br />

As mentioned earlier, themes must be applied in the Page_PreInit() event h<strong>and</strong>ler. This isn’t accessible<br />

from the master page that all pages use, so if you want to apply a selected theme to all pages, you are left<br />

with two options:<br />

➤<br />

➤<br />

Override the Page_PreInit() event h<strong>and</strong>ler in all pages where you want themes to be applied.<br />

Provide a common base class for all pages where you want themes to be applied, <strong>and</strong> override the<br />

Page_PreInit() event h<strong>and</strong>ler in this base class.<br />

PCSDemoSite uses the second option, with a common page base class provided in Code/MyPageBase.cs:<br />

public class MyPageBase : Page<br />

{<br />

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

{<br />

// theming<br />

if (Session["SessionTheme"] != null)<br />

{<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!