Sunday, November 30, 2008

Enable Themes in you Web Site

I recently had to implement Themes in a Web Site that I am working on and found out that the .net framework has made it very easy to implement. Here is how I did it :

In my default.aspx page on the OnPreInit method (overridden) I simply add this :

protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        if (Session["ThemeName"] == null)
        {
            Session.Add("ThemeName", "Green");
            Page.Theme = ((string)Session["ThemeName"]);
        }
        else
        {
            Page.Theme = ((string)Session["ThemeName"]);
        }
    }

Then from anywhere on the page I simply call this :

if (Session["MyTheme"] == null)
        {
            Session.Add("ThemeName", "Black");
        }
        else
        {
            if (((string)Session["ThemeName"]) == "Green")
            {
                Session["ThemeName"] = "Black";
            }

            else
            {
                Session["ThemeName"] = "Green";
            }
        }

Hope this helps, 

 - Tim