
This free ThemeSwitcher control allows the user to choose one of the available themes (ASP.NET 2.0). The theme is applied automatically to all ASP.NET pages in the web site, and is made persistent through the use of a cookie.
You need to do three things to use this control in your web site:
Add the multiple themes to your application in the usual way.
In Visual Studio 2005
you can add a new theme by right clicking on the root of the application, selecting
"Add ASP.NET Folder", followed by "Theme". In the theme folder, you can add style
sheet files (.css), skin files (.skin), images, etc.
Repeat the same procedure for each additional theme.
If you want to provide a default theme, just create a new theme folder with the
name "Default".
If a folder with that name exists, it will be used as the default theme by the
application, until the user selects another theme with the theme switcher.
If you don't provide a default theme, the user will get no themes on his pages
until he selects a particular theme.
In web.config, you need to add this code to the <system.web> element:
<httpModules>
<add type="rw.ThemeSwitcherModule, ThemeSwitcher" name="ThemeSwitcherHttpModule" />
</httpModules>
WARNING: this setting causes a server error in every page having a <head> tag without the runat="server" attribute! Make sure this attribute is set in every aspx page of your site. By default, Visual Studio 2005 and Visual Web Developer add this attribute to every page, but you may still have pages that were created for ASP.NET 1.x, when this attribute was not needed.
Typically, you could create a single page for the user to select a theme. You need to add the control to this page only. As soon as the user selects a theme from the list, this theme will be added to all the pages in the entire site.
As an alternative, you could add the theme switcher to the master page, making the theme selection available on every page in the site.
Before using the control in a page, you can install it in the standard way:
You may want to add the control to the toolbox of your editor. This will allow you to set its properties in the Property Pane. Follow the editor's procedure to add a control to the toolbox.
There are 2 ways to add the control to your page or master page:
<%@ Register TagPrefix="rw" Namespace="rw" Assembly="ThemeSwitcher" %>Then,
add a tag like this where you want the theme list to be displayed: <rw:ThemeSwitcher id="ThemeSwitcher1" runat="server" >/>You can check the demo file demo.aspx for a sample.
Finally, you can set three properties:
<appSettings>
<add key="DefaultThemeName" value="Mono"/>
</appSettings>
The code is composed of two parts, one for the theme list control, and one for the Http module handling the theme selection.
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
Me.ID = "lbThemeSwitcher" ' force an ID, so we can use it in HttpModule
If (Not Page.IsPostBack) Then
' AutoPostBack will be handled in HttpModule
Me.AutoPostBack = True
If (Me.AllowNoTheme) Then
' First item is "none"
Items.Add(New ListItem(NoThemeText, "0"))
End If
If (Directory.Exists(Page.MapPath("~/App_Themes"))) Then
' Enumerate the subfolders of the themes folder and add them to the list
Dim subdirs() As String = Directory.GetDirectories(Page.MapPath("~/App_Themes"))
For Each dir As String In subdirs
Dim dirInfo As New DirectoryInfo(dir)
Items.Add(dirInfo.Name)
Next
End If
End If
' set selection to current value of the theme
SelectedIndex = -1
If (Page.Theme Is Nothing OrElse Page.Theme = "") Then
SelectedIndex = 0
Else
Dim li As ListItem = Items.FindByValue(Page.Theme)
If (li IsNot Nothing) Then li.Selected = True
End If
End Sub
Protected Sub PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
Dim CurrentContext As HttpContext = HttpContext.Current
If (Not TypeOf CurrentContext.Handler Is Page) Then Return
Dim myPage As Page = CurrentContext.Handler
If Not myPage Is Nothing Then
' This 8 lines below handle master pages. Courtesy of Craig G Fraser
Dim sUniqueID As String = "lbThemeSwitcher"
Dim sCtrlID As String
For Each sCtrlID In CurrentContext.Request.Form.AllKeys
If sCtrlID.Contains("lbThemeSwitcher") Then
sUniqueID = sCtrlID
Exit For
End If
Next
Dim theme As Object = CurrentContext.Request.Form(sUniqueID)
If (Not theme Is Nothing) Then
' this is postback from the page with the theme switcher list
' handle the user selection in the theme list
If (theme.ToString() = "0") Then
' user chose "none"
myPage.Theme = ""
' delete the cookie
CurrentContext.Response.Cookies(CookieName()).Expires = DateTime.Today.AddDays(-1)
Return
End If
If (ThemeExists(theme)) Then myPage.Theme = theme
' set a cookie for persistence
CurrentContext.Response.Cookies(CookieName()).Value = theme
CurrentContext.Response.Cookies(CookieName()).Expires = DateTime.Today.AddDays(90) ' 90 days
Else
' for other pages with no theme switcher on them
Dim cookie As Object = CurrentContext.Request.Cookies(CookieName())
If (Not cookie Is Nothing AndAlso cookie.Value <> "") Then
' if there's a cookie, get the theme from the cookie
If (ThemeExists(cookie.Value)) Then myPage.Theme = cookie.Value
cookie.Expires = DateTime.Today.AddDays(90) ' reset expiration date
Else
' if there's no cookie, select the default theme (if it exists)
' the developer should provide a theme with the name "Default"
' if this behavior is wanted
If (ThemeExists("Default")) Then myPage.Theme = "Default"
Dim DefaultThemeName As String = System.Configuration.ConfigurationManager.AppSettings("DefaultThemeName")
If (DefaultThemeName <> "" And ThemeExists(DefaultThemeName)) Then myPage.Theme = DefaultThemeName
End If
End If
End If
End Sub
AllowNoTheme property