Dynamic Themes in ASP.NET 2.0 (C#)
A step by step guide to create dynamic themes in ASP.NET 2.0.
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
- Step 1: Under the App_Code folder, we add a class file named Theme.cs:
- 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. - 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 theBasePage
class in Step 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.
- Step 5: In the master page code-behind, such as Default.master.cs, add these methods:
- Step 6: Add the
BasePage
class under App_Code, and specify the default theme. Here, we use "White
". - Step 7: Inherit all the pages using the dynamic theme from
BasePage
:
public class Theme
{
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
public Theme(string name)
{
Name = name;
}
}
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
}
<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 />
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);
}
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"]);
}
}
}
public partial class Default_aspx : BasePage
{
}
Happy programming!