Tips Station Asp.net Article Seo Articles
Tutorials Code Samples
›  Home
›  Mission
›  About us
›  Contact Us
›  Feedback
›  Terms & Condition
Asp Articles
IT Solutions
 
› ASP.NET

› Programming Tips

› Ajax

› Asp

› ADO.NET

› Databases

› SEO

› CSS And Designing

› Php

 
Most Viewed Articles
 
› Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

› Change theme dynamically without page refresh in ASP.NET

› Install AJAX On Machines Running Visual Studio 2005

› Creating Pretty Popups Using AJAX

› Simple ASP Image Resize Function

› SQL Server Performance Counters

› ASP.Net Interview Questions And Their Answers

› Encode Url using ASP

› Difference Between DataGrid and GridView in Asp.Net

› Select Specific Value WithIn Drop Down List Or Radio Button List

more...
 
 
Home> ASP.NET
 
Change theme dynamically without page refresh in ASP.NET
 
You must have noticed one link in yahoo.com, msn.com or other popular websites named "Page Options". When you click this link you get a popup displaying different small several color icons. After clicking one of these icons your page theme changes without entire page refresh. Now you are able to see the same page with different look and feel.

How does it happen and what it takes to do it? Is this possible in ASP.NET? If yes, how to do it? Do I need to use personalizaton? .... and so on. There are many questions arises in our mind when we try to do like this in asp.net.


Let me walk you through the process of coding this.


Step 1 - ChangeThemeControl.ascx

Create a user control that will have links of different themes and a simple JavaScript function named ChangeTheme(theme, themeName) that will take two parameter.

theme: This is the path of the .css file that will be used to temporarily set the theme of the page from which you have opted to change the theme.

themeName: This is the actual theme name that you have created in App_Themes folder of your root directory.

Put one <iframe> on this user control. This iframe will be used to pass the theme name as a querystring to ChangeThemeHidden.aspx page (described next) when you click on different theme icons/links.

The major code of this usercontrol is
<hr />

Change theme: <a href="#" onclick="javascript:ChangeTheme('/theme1/theme1.css', 'theme1')">Theme 1</a> |

<a href="#" onclick="javascript:ChangeTheme('/theme2/theme2.css', 'theme2')">Theme 2</a>

<hr />

<asp:Label ID="Label1" runat="server"></asp:Label>



 <iframe id="magicFrame" width="0" height="0" style="display:none;"></iframe>



<script language="javascript" type="text/javascript">

function ChangeTheme(theme, themeName)

        {

            var cssid = document.getElementsByTagName("link");

            var css = '';

            for (var j = 0; j < cssid.length ; j++)

            {

                if (cssid[j].type == 'text/css') // search only css link

               {

                    cssid[j].href = "/App_Themes"+ theme;

                   

                    css = cssid[j].href;

                }

            }

           

            document.getElementById('magicFrame').src = 'ChangeThemeHidden.aspx?theme='+ themeName;

          

        }

</script>


Step 2 - ChangeThemeHidden.aspx

Create a separate page ChangeThemeHidden.aspx that will contain nothing but a Page_Load event.

This Page_Load event will check for a querystring called theme, if it will find this querystring then it will write its value to the session variables Session["GlobalSessionTheme"]. This session variable will be used to persist the theme name for that particular user who is visiting your site.

The code of this file is
protected void Page_Load(object sender, EventArgs e)

    {

        if (Request["theme"] != null)

        {

            WriteTheme(Request["theme"].ToString());

        }

    }



    private void WriteTheme(string theme)

    {

        Session["GlobalSessionTheme"] = theme;

    }


Step 3 - ChangeTheme.aspx & ChangeTheme2.aspx

Just create two sample pages to see that your code is working fine and selected theme is persisting even if user is navigating from one page to another. On this page, just register the usercontrol that you created in the first step, this will give option to the user to select different themes from any page of the site.

These pages may contain anything you need on it. In addition to your own code you will need to write following event to make sure that you are rendering the page with the theme that was selected by user.

The code to do this is
protected override void OnPreInit(EventArgs e)

    {

        if (Session["GlobalSessionTheme"] != null)

            this.Theme = Session["GlobalSessionTheme"].ToString();

    }



Step 4 - Theme1.css, Theme2.css

Create your .css files for different themes. I have create two files with different background & foreground color of the page.

Theme1.css
body

{

        background-color:yellow;

       

        font-size:10pt;

        color:Blue;

}



a

{

        font-weight:bold;

        color:Green;  

}


Theme2.css
body

{

        background-color:#c0d0e0;

       

        font-size:10pt;

        color:Maroon;

}



a

{

        font-weight:bold;

        color:blue;   

}


 
Step 5 - Just test it!!!


Browse ChangeTheme.aspx page and click on Theme 2 link, your current page theme will be changed to Theme 2 look and feel (Did u noticed that the whole page is not posted back to the server? , hmm... happy?). Now, when you will click on Page 2 link, the browser will be redirected to ChangeTheme2.aspx page and the look and feel of the page will remain same (Theme 2).
 
Try doing vice-versa and you will see that when you are changing the theme, page is not getting refreshed and your theme is persisting.

 
 
Vrp Technologies
 
Serversea Hosting
 
 
Latest Articles
 
› Sending SMS With PHP

› MySQL Join Tutorial

› Make An RSS Feed Using PHP

› Intro To Object: Option Variables

› Design An Online Chat Room With PHP And MySQL

› Create Tell A Friend Script With HTML & PHP

› Benchmark And Optimize PHP Script Speed

› What Kind of DBA Are You?

› SQL Server Performance Counters

› SQL Server Performance Tips

more...
 
Random Articles
 
› Storing and Retrieving Variables From Application Object - Asp

› ADO.NET Data Classes

› Search Engine Optimization & Submission Strategies - A Beginners Guide

› ASP.NET Simple File Upload Function

› How to connect to a database via a DSN-Less connection - ASP

› How to export a GridView to Excel - Asp.net

› Access Master Page controls from the Content Page - Asp.net

› What Are Active Server Pages (ASP)?

› Server Variables in ASP

› 8 Useful SEO Techniques Every Webmaster Should Know

more...
 
Home Mission About us Contact us Feedback Terms Conditions
2008 © Copyright TipsStation. All rights reserved.