5,443,978 members and growing! (21,041 online)
Email Password   helpLost your password?
Web Development » ASP.NET » Howto     Intermediate License: The Code Project Open License (CPOL)

Dynamic Themes in ASP.NET 2.0 (C#)

By Sue_NC

A step by step guide to create dynamic themes in ASP.NET 2.0.
CSS, C#, Windows, .NET, Visual Studio, ASP.NET, Dev

Posted: 19 Jun 2006
Updated: 19 Jun 2006
Views: 60,047
Bookmarked: 61 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
30 votes for this Article.
Popularity: 6.83 Rating: 4.62 out of 5
0 votes, 0.0%
1
2 votes, 6.7%
2
2 votes, 6.7%
3
4 votes, 13.3%
4
22 votes, 73.3%
5

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. 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(
              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: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);
        Server.Transfer(Request.FilePath);
    }
  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)
        {
            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. 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)

About the Author

Sue_NC


http://www.edream.org/resume.aspx
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 31 (Total in Forum: 31) (Refresh)FirstPrevNext
Subject  Author Date 
QuestionPlease help me....memberAbhishek sur2:10 14 Nov '07  
GeneralDynamic Themesmemberwebert111:13 25 Oct '07  
GeneralProfiles are better than sessionsmemberwali1017:01 17 Oct '07  
Questionpostbackmemberiron1063:59 23 Jul '07  
QuestionHelp me! Server.MapPathmembercuongnp15:23 3 Jul '07  
AnswerRe: Help me! Server.MapPathmembercuongnp18:45 3 Jul '07  
GeneralAny way to persist the statememberLiquid Len10:25 30 Apr '07  
QuestionExamplememberM-Daraghmeh2:07 8 Apr '07  
Generalhamed ( Elixir )memberhamedkocholo0:22 1 Apr '07  
QuestionUploading a new theme/skinmemberXadhoomGrglblstr9:56 29 Jan '07  
AnswerRe: Uploading a new theme/skinmemberJuba10:06 22 Feb '07  
GeneralNice ArticlememberManish Pansiniya19:51 13 Dec '06  
Generalcool!memberenteng.kabisote16:51 7 Nov '06  
GeneralHow about using PreInit of master page?memberAddisu H. Desta7:09 31 Oct '06  
GeneralRe: How about using PreInit of master page?memberKoaQiu1:07 11 Apr '07  
Generalviemembervievae2:57 30 Oct '06  
GeneralThank you! And a small tipsupporterJohn Cardinal15:06 11 Oct '06  
Generalgreat and many thanksmemberjackie1239:39 15 Sep '06  
GeneralNice article (& suggestion)membermsmits4:00 12 Jul '06  
GeneralGreat Articlemembergweeter10:46 30 Jun '06  
GeneralRe: Great ArticlememberArrez4:36 28 Jul '06  
GeneralGood, but...memberGonzalo Brusella3:28 27 Jun '06  
GeneralRe: Good, but...memberAbishek Bellamkonda15:36 28 Jun '06  
GeneralNo BasePagememberDeKale1:38 20 Jun '06  
GeneralRe: No BasePagememberMike Ellison7:04 20 Jun '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Jun 2006
Editor: Smitha Vijayan
Copyright 2006 by Sue_NC
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project