Friday 16 March 2012

Creating User-Selectable Themes

Problem 
I am using MasterPages and I want to allow to the user to change the Themes of the web pages with minimal effort.

Impact
It will which let you add personalization to the web site with different appearance and achieve a visual consistency.


Solution
You can use different Themes to customize your website.  We use two different ways:

1.      Using sessions:

  protected void Page_PreInit(object sender, EventArgs e)
  {
      if (Session["theme"] != null)
      {
          Page.Theme = (string)Session["theme"];
      }
  } 

  protected void ButtonSubmit_Click(object sender, EventArgs e)
  {
      string theme = Page.Theme;
      string test = (string)Session["theme"];
      TextBox masterButton = (TextBox)Master.FindControl("Button1");

      //switch themes
      if (theme != "PurpleTheme")
      {
            Session["theme"] = "PurpleTheme";
            masterButton.Text = "Change to Pink";
      }
      else
      {
            Session["theme"] = "RedTheme";
            masterButton.Text = "Change to Purple";
      }
  }

2.      Using different MasterPages: This is the content page, which allows users to select a master page based on a query string provided with the request. The @ MasterType directive, which assigns a strong type to the page's Master property, references the base type.
<%@ Page Language="C#" Title="Content Page" MasterPageFile="~/MasterPink.master"%>
<%@ MasterType TypeName="BaseMaster" %>


<script runat="server">

    protected void Page_PreInit(Object sender, EventArgs e)
    {
        this.MasterPageFile = " MasterPink.master";
        if(Request.QueryString["color"] == "purple")
        {
            this.MasterPageFile = "MasterPruple.master";
        }
        this.Title = Master.MyTitle;
    }

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

Conclusion
In the example I show two way in order to change the Themes of the website by using sessions and different MasterPages. You can use a theme property in a user profile to make a user's theme selection persist after the user leaves your site or closes the browser.

No comments:

Post a Comment