Click here to Skip to main content
15,886,873 members
Articles / Web Development / CSS
Article

Dynamic Themes in ASP.NET 2.0 (C#)

Rate me:
Please Sign up or sign in to vote.
4.79/5 (49 votes)
19 Jun 2006CPOL1 min read 437.7K   5.9K   143   44
A step by step guide to create dynamic themes in ASP.NET 2.0.

Sample Image - dynamicThemes.jpg

Introduction

ASP.NET 2.0 makes dynamic themes really easy. No need to envy someone having cool multiple themes, you can have your own instantly! This article gives you step by step instructions on how to make dynamic themes in C#. You can try this code out with the Personal Web Site Starter Kit.

The result

Live demo is available here. Click on "change themes" on the top left corner, under the logo.

The code

  1. Step 1: Under the App_Code folder, we add a class file named Theme.cs:
  2. C#
    public class Theme
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
        public Theme(string name)
        {
            Name = name;
        }
    }
  3. Step 2: Under the App_Code folder, we add a ThemeManager class file named ThemeManager.cs. This will list all the available themes under the /App_Themes folder.
  4. C#
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    public class ThemeManager
    {
    #region Theme-Related Method
        public static List<Theme> GetThemes()
        {
            DirectoryInfo dInfo = new DirectoryInfo(
              System.Web.HttpContext.Current.Server.MapPath("App_Themes"));
            DirectoryInfo[] dArrInfo = dInfo.GetDirectories();
            List<Theme> list = new List<Theme>();
            foreach (DirectoryInfo sDirectory in dArrInfo)
            {
                Theme temp = new Theme(sDirectory.Name);
                list.Add(temp);
            }
            return list;
        }
    #endregion
    }
  5. Step 3: Comment out any pre-defined themes such as <!--<pages styleSheetTheme="Black"/>--> in the web.config. You don't need this because the application level default theme will be specified in the BasePage class in Step 6.
  6. Step 4: In you master page, such as Default.master, add a data source and a radiobutton list. You can use a dropdownlist if you would prefer that.
  7. ASP.NET
    <asp:ObjectDataSource ID="ThemeDataSource" runat="server" 
     SelectMethod="GetThemes" TypeName="ThemeManager" ></asp:ObjectDataSource>
    <asp:RadioButtonList ID="strTheme" runat="server" DataTextField=name 
     DataValueField=name OnSelectedIndexChanged="strTheme_SelectedIndexChanged" 
     OnDataBound="strTheme_DataBound" DataSourceID="ThemeDataSource" 
     AutoPostBack=true RepeatDirection=Horizontal />
  8. Step 5: In the master page code-behind, such as Default.master.cs, add these methods:
  9. C#
    protected void strTheme_DataBound(object sender, EventArgs e)
    {
        strTheme.SelectedValue = Page.Theme;
    }
    protected void strTheme_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session.Add("MyTheme", strTheme.SelectedValue);
        Server.Transfer(Request.FilePath);
    }
  10. Step 6: Add the BasePage class under App_Code, and specify the default theme. Here, we use "White".
  11. C#
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    
    public class BasePage : System.Web.UI.Page
    {
        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);
            if (Session["MyTheme"] == null)
            {
                Session.Add("MyTheme", "White");
                Page.Theme = ((string)Session["MyTheme"]);
            }
            else
            {
                Page.Theme = ((string)Session["MyTheme"]);
            }
        }
    }
  12. Step 7: Inherit all the pages using the dynamic theme from BasePage:
  13. C#
    public partial class Default_aspx : BasePage
    {
    }

Happy programming!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
http://www.edream.org/resume.aspx

Comments and Discussions

 
Generalcool! Pin
enteng.kabisote7-Nov-06 15:51
enteng.kabisote7-Nov-06 15:51 
QuestionHow about using PreInit of master page? Pin
Addisu H. Desta31-Oct-06 6:09
Addisu H. Desta31-Oct-06 6:09 
AnswerRe: How about using PreInit of master page? Pin
KoaQiu11-Apr-07 0:07
KoaQiu11-Apr-07 0:07 
Generalvie Pin
vievae30-Oct-06 1:57
vievae30-Oct-06 1:57 
GeneralThank you! And a small tip Pin
Member 9611-Oct-06 14:06
Member 9611-Oct-06 14:06 
Generalgreat and many thanks Pin
jackie12315-Sep-06 8:39
jackie12315-Sep-06 8:39 
GeneralNice article (& suggestion) Pin
msmits12-Jul-06 3:00
msmits12-Jul-06 3:00 
GeneralGreat Article Pin
gweeter30-Jun-06 9:46
gweeter30-Jun-06 9:46 
Thank you for posting this information. I looked in several places, but none had this approach, which is what I was looking for.

I do have one thing to add for anyone else who might want to change the StyleSheetTheme instead of just the Theme.
In the Base.cs class I had to override the StyleSheetTheme property instead of the OnPreInit method.

Like this:

private string myTheme;
public override string StyleSheetTheme
{
get
{
if (Session["MyTheme"] == null)
{
return "ThemeName";
}
else
{
return (string)Session["MyTheme"];
}
}
set
{
myTheme = value;
}
}

Hope that helps any future visitors!

GeneralRe: Great Article Pin
Arrez28-Jul-06 3:36
Arrez28-Jul-06 3:36 
GeneralGood, but... Pin
Gonzalo Brusella27-Jun-06 2:28
Gonzalo Brusella27-Jun-06 2:28 
GeneralRe: Good, but... Pin
Abi Bellamkonda28-Jun-06 14:36
Abi Bellamkonda28-Jun-06 14:36 
GeneralNo BasePage Pin
DeKale20-Jun-06 0:38
DeKale20-Jun-06 0:38 
GeneralRe: No BasePage Pin
Mike Ellison20-Jun-06 6:04
Mike Ellison20-Jun-06 6:04 
GeneralRe: No BasePage Pin
DeKale20-Jun-06 10:29
DeKale20-Jun-06 10:29 
GeneralInteresting, but with problems Pin
NinjaCross19-Jun-06 22:48
NinjaCross19-Jun-06 22:48 
GeneralRe: Interesting, but with problems Pin
Sue_NC20-Jun-06 1:21
Sue_NC20-Jun-06 1:21 
GeneralIt changed only once... Pin
ilog.km19-Jun-06 22:33
ilog.km19-Jun-06 22:33 
GeneralRe: It changed only once... Pin
ilog.km19-Jun-06 22:35
ilog.km19-Jun-06 22:35 
GeneralRe: It changed only once... Pin
Sue_NC20-Jun-06 1:22
Sue_NC20-Jun-06 1:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.