Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Dynamically changing themes

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 245
    Comment on it

    This blog gives you step by step instructions on how to make dynamic themes in C#.
    1) First develop all of the themes you want and put them all under the App_Themes folder in your ASP.Net project. I have created two themes with Red and Green Name.

     //css code for green theme 
    body
    {
        background-color:green;
        color:White;
        font-family: Calibri, Verdana, Arial, 'Times New Roman';
        font-size: 12pt;
    } 
    
     //css code for red theme 
    body
    {
        background-color:red;
        color:White;
        font-family: Calibri, Verdana, Arial, 'Times New Roman';
        font-size: 12pt;
    } 
    


    2) Create a default page.
    I have used one drop down control for theme selection.

    <pre>
    <asp:DropDownList ID="ddlTheme"  AutoPostBack="true" onchange="javascript:ChangeTheme();"  runat="server">
                  <asp:ListItem>--Please Select--</asp:ListItem>
                <asp:ListItem >Green</asp:ListItem>
                <asp:ListItem>Red</asp:ListItem>
            </asp:DropDownList>
    </pre>
    

    During dynamically changing themes "No Need to use any global variable, session and no need to send the new request to server only postback is required means no need to use Response.Redirect to same or other pages". I simply store the selected theme in client cookie at client side by using javascript function and load newly selected theme on page preinit event at server side.

    Java script Function:--

     function ChangeTheme()
        {
            var todayDate = new Date();
            todayDate.setTime(todayDate.getTime() + (60 * 60 * 1000 * 24));
            document.cookie = "theme=" + document.getElementById('ddlTheme').value + ";expires=" +    todayDate.toGMTString();
        }
    

    3) On default.cs please write the below code.

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if ((Request.Cookies["theme"] != null) && (Request.Cookies["theme"].Value.Length != 0))
                    ddlTheme.Text = Request.Cookies["theme"].Value;
            }
        }
    
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if ((Request.Cookies["theme"] != null) && (Request.Cookies["theme"].Value.Length !=0))
                Page.Theme = Request.Cookies["theme"].Value;
            else
                Page.Theme = "Green";
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: