Theme Switcher Control(转载)

原文地址:http://www.codeproject.com/useritems/themeswitcher.asp
Introduction

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.

Try the online demo

Using the control

You need to do two things to use this control in your web site:

  1. Add the Http Module handler to web.config
  2. Create a page for the user to select a theme, and add the theme switcher control to this page.

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.

Before using the control in a page, you can install it in the standard way:

  • create a "bin" folder inside the application root of your website (if it's not already there)
  • copy the assembly file themeswitcher.dll into the bin folder

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.

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 list to the master page, making the theme selection available on every page in the site.

There are 2 ways to add the control to your page or master page:

  1. Drag the control from the toolbox onto the page (if it was installed on the toolbox).
  2. Add the code manually. Add this line to the top of your 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, set the NoThemeText property if you don't want to use the default text "no theme".

How it works

The code is composed of two parts, one for the theme list control, and one for the Http module handling the theme selection.

Theme list code

This is a control derived from the ListBox control. In the Page_Load event, the control will enumerate all the subfolders of the App_Themes folder of the site. The names of these subfolders are also the names of the available themes. This is the code for the OnLoad handler:
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
' First item is "No theme"
Items.Add(New ListItem(NoThemeText, "0"))
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

Http Module code

This is a class implementing the IHttpModule interface. It will intercept every page request, and automatically apply the selected theme. This will be done in the PreRequestHandler:
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
Dim theme As Object = CurrentContext.Request.Form("lbThemeSwitcher")
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("tsTheme").Expires = DateTime.Today.AddDays(-1)
Return
End If
myPage.Theme = theme
' set a cookie for persistence
CurrentContext.Response.Cookies("tsTheme").Value = theme
CurrentContext.Response.Cookies("tsTheme").Expires = DateTime.Today.AddDays(90) ' 90 days
ElseIf (Not CurrentContext.Request.Cookies("tsTheme") Is Nothing) Then
' for every other page, get the theme from the cookie
myPage.Theme = CurrentContext.Request.Cookies("tsTheme").Value
End If
End If
End Sub

Points of interest

  • Using IHttpModule
  • ASP.NET 2.0 themes
posted @ 2006-04-18 21:40  shaomin's Knowledge Base  阅读(217)  评论(0)    收藏  举报