Click here to Skip to main content
15,892,674 members
Articles / Web Development / ASP.NET
Article

Enable Theming using Cookies

Rate me:
Please Sign up or sign in to vote.
1.36/5 (4 votes)
14 Aug 2006 17.4K   9  
Save User Theming Choice in Cookie

Introduction

In converting my application to .Net 2.0, I wanted to include theme. But I asked myself how I change the theme in sub pages. I have a master page, but I wanted to be able to save the theme the user selected somewhere also. So I decided to use and create a Base Class file which inherits the System.Web.UI.Page. So the tasks is let the user change a theme on my website and save the choice in a cookie. So next time they come back, the theme they choose will be there.



Base Class:

protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
String ThemeName;
if (Master != null)
{
_masterPageOrg = Page.MasterPageFile.ToString();
}

if (Request.Cookies["MyThemes"] != null)
{
ThemeName = Request.Cookies["MyThemes"].Value;
}
else
{
return;
}
switch (ThemeName)
{
case "Green":
Page.Theme = "Green";
break;
case "Orange":
Page.Theme = "Orange";
break;
case "Purple":
Page.Theme = "Purple";
break;
case "Blue":
Page.Theme = "Blue";
break;
case "Red":
Page.Theme = "Red";
break;
case "Yellow":
Page.Theme = "Yellow";
break;
}
}



Master Page Code:

SelTheme.Value = "Orange";
SelTheme.Expires = DateTime.Now.AddMonths(12);
if (Response.Cookies["MyThemes"] == null)
{
Response.Cookies.Add(SelTheme);
}
else
{
Response.Cookies.Remove("MyThemes");
Response.Cookies.Add(SelTheme);
}


View Source Code

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --