Click here to Skip to main content
Click here to Skip to main content
Go to top

Dynamic Themes in ASP.NET 2.0 (C#)

, 19 Jun 2006
Rate this:
Please Sign up or sign in to vote.
A step by step guide to create dynamic themes in ASP.NET 2.0.

Sample Image - dynamicThemes.jpg


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. 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. 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(
            DirectoryInfo[] dArrInfo = dInfo.GetDirectories();
            List<Theme> list = new List<Theme>();
            foreach (DirectoryInfo sDirectory in dArrInfo)
                Theme temp = new Theme(sDirectory.Name);
            return list;
  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: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. 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);
  10. Step 6: Add the BasePage class under App_Code, and specify the default theme. Here, we use "White".
  11. 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)
            if (Session["MyTheme"] == null)
                Session.Add("MyTheme", "White");
                Page.Theme = ((string)Session["MyTheme"]);
                Page.Theme = ((string)Session["MyTheme"]);
  12. Step 7: Inherit all the pages using the dynamic theme from BasePage:
  13. public partial class Default_aspx : BasePage

Happy programming!


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


About the Author


United States United States

Comments and Discussions

QuestionThemes PinmemberRama Sagar Pulidindi3-Jun-13 19:32 
Questionvote of 5 PinmemberMember 878749724-Aug-12 7:45 
GeneralMy vote of 5 PinmemberMd. Mainuddin30-Nov-11 4:06 
QuestionMy Vote of 5 PinmemberPraveen Kullu27-Nov-11 23:32 
GeneralMy Vote of 5 PinmemberRaviRanjankr20-Nov-11 20:46 
GeneralMy vote of 5 PinmemberNiranjankumar from Patna15-Nov-11 0:10 
GeneralDemo link is broken PinmemberMichael Freidgeim17-Jun-11 17:21 
GeneralMy vote of 1 Pinmembersheradhakar17-Feb-11 23:02 
GeneralReally nice article.... best demonstration. PinmemberPrathameshP23-Dec-09 23:18 
GeneralPerfect! Pinmembernetizenk18-Jun-09 12:05 
Generaldynamic web c# Pinmembermagdyeltahaan26-May-09 22:08 
QuestionChange look and feel of portions of the page based on selected theme. Pinmembersudipta.india24-Nov-08 21:35 
QuestionPlease help me.... PinmemberAbhishek sur14-Nov-07 1:10 
GeneralDynamic Themes Pinmemberwebert125-Oct-07 10:13 
GeneralProfiles are better than sessions Pinmemberwali10117-Oct-07 6:01 
Questionpostback Pinmemberiron10623-Jul-07 2:59 
QuestionHelp me! Server.MapPath Pinmembercuongnp3-Jul-07 14:23 
AnswerRe: Help me! Server.MapPath Pinmembercuongnp3-Jul-07 17:45 
GeneralAny way to persist the state PinmemberLiquid Len30-Apr-07 9:25 
GeneralRe: Any way to persist the state [modified] PinmemberD. Kuzmanovic4-Dec-08 22:33 
QuestionExample PinmemberM-Daraghmeh8-Apr-07 1:07 
Generalhamed ( Elixir ) Pinmemberhamedkocholo31-Mar-07 23:22 
QuestionUploading a new theme/skin PinmemberXadhoomGrglblstr29-Jan-07 8:56 
AnswerRe: Uploading a new theme/skin PinmemberJuba22-Feb-07 9:06 
GeneralNice Article PinmemberManish Pansiniya13-Dec-06 18:51 
Generalcool! Pinmemberenteng.kabisote7-Nov-06 15:51 
QuestionHow about using PreInit of master page? PinmemberAddisu H. Desta31-Oct-06 6:09 
AnswerRe: How about using PreInit of master page? PinmemberKoaQiu11-Apr-07 0:07 
Generalvie Pinmembervievae30-Oct-06 1:57 
GeneralThank you! And a small tip PinmemberJohn Cardinal11-Oct-06 14:06 
Generalgreat and many thanks Pinmemberjackie12315-Sep-06 8:39 
GeneralNice article (& suggestion) Pinmembermsmits12-Jul-06 3:00 
GeneralGreat Article Pinmembergweeter30-Jun-06 9:46 
GeneralRe: Great Article PinmemberArrez28-Jul-06 3:36 
GeneralGood, but... PinmemberGonzalo Brusella27-Jun-06 2:28 
GeneralRe: Good, but... PinmemberAbishek Bellamkonda28-Jun-06 14:36 
GeneralNo BasePage PinmemberDeKale20-Jun-06 0:38 
GeneralRe: No BasePage PinmemberMike Ellison20-Jun-06 6:04 
GeneralRe: No BasePage PinmemberDeKale20-Jun-06 10:29 
GeneralInteresting, but with problems PinmemberNinjaCross19-Jun-06 22:48 
GeneralRe: Interesting, but with problems PinmemberSue_NC20-Jun-06 1:21 
GeneralIt changed only once... Pinmemberilog19-Jun-06 22:33 
GeneralRe: It changed only once... Pinmemberilog19-Jun-06 22:35 
GeneralRe: It changed only once... PinmemberSue_NC20-Jun-06 1:22 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140922.1 | Last Updated 19 Jun 2006
Article Copyright 2006 by Sue_NC
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid