Theme: default | dark | nature

MasterThemeModule

This module will check to see if you have a cookie named theme defined. If so, it will set the page theme to the value of the cookie. You can easily change it to accept a value from the query string or post data but this works best.

First, attach the module to your web site by adding this entry to your web.config.

<?xml version="1.0"?>
<system.web>
    <httpModules>
      <add name="MasterThemeModule" type="MasterThemeModule, App_Code" />
    </httpModules>
...
</system.web>

Now, copy this code to /App_Code/MasterThemeModule.vb.

Imports System.Web
Imports System.Web.UI

Public Class MasterThemeModule : Implements IHttpModule
    Private _application As HttpApplication

    Public Sub Init(ByVal app As System.Web.HttpApplication) Implements IHttpModule.Init
        AddHandler app.PreRequestHandlerExecute, AddressOf Application_PreRequestHandlerExecute
    End Sub

    Public Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
        _application = DirectCast(sender, HttpApplication)

        Dim page As Page = TryCast(_application.Context.CurrentHandler, Page)

        If (Not IsNothing(page)) Then AddHandler page.PreInit, AddressOf Page_PreInit
    End Sub

    Public Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
        Dim page As Page = DirectCast(sender, Page)

        If (page.EnableTheming) Then
            If (_application.Context.Request.Cookies("theme") IsNot Nothing) Then theme = _application.Context.Request.Cookies("theme").Value
            If (String.IsNullOrEmpty(theme)) Then theme = "Default"
            page.Theme = theme
        End If
    End Sub

    Public Sub Dispose() Implements IHttpModule.Dispose
    End Sub
End Class

Contact us today! Free consulting with no obligation.