Friday, July 8, 2011

Switch Theme Without Postback


using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 partial class ChangeTheme : System.Web.UI.Page, ICallbackEventHandler
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        //If theme is null or not changed by user use Default theme
        if (String.IsNullOrEmpty(Convert.ToString(Session["Theme"])))
            Page.Theme = "Default";
        else
            Page.Theme = Convert.ToString(Session["Theme"]);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //JavaScript Callbacks
        ClientScriptManager scriptManager = Page.ClientScript;
        String cbReference = scriptManager.GetCallbackEventReference(this, "returnValue", "ReceiveServerData", "context");
        String callbackScript = "function CallServer(returnValue, context) {" + cbReference + "; }";
        scriptManager.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);

        TextBox1.Text = DateTime.Now.ToString();
    }

    #region ICallbackEventHandler Members
    //Return value from the server to the client
    string ICallbackEventHandler.GetCallbackResult()
    {
        return Convert.ToString(Session["Theme"]);
    }

    //Get the theme name from the javascript function
    public void RaiseCallbackEvent(string eventArgument)
    {
        Session["Theme"] = eventArgument.Trim();
    }

    #endregion
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ChangeTheme.aspx.cs" 
Inherits="ChangeTheme" %>




    Theme Switcher
    
    
    

    


    

The style set for the background body and the textbox.

You can see that, the time is not changed when we select the color.
This is done from the javascript.
To make apply the theme press the button Apply Theme.



Source Code

No comments: