Click here to Skip to main content
6,629,885 members and growing! (22,200 online)
Email Password   helpLost your password?
Web Development » HTML / CSS » General     Beginner License: The Code Project Open License (CPOL)

How to change page theme in asp.net 2.0 dynamically at runtime

By Rajesh Naik Ponda Goa

ASP.Net has introduced one neat feature called themes by virtue of which we can assign different themes to page at design time. Unfortunately there is not built in support to change page themes at runtime. Here is a simple code which can be used to change page themes at runtime.
HTML, C# 2.0, .NET CF, .NET 2.0, Win2K, WinXP, Win2003, Vista, .NET 3.0, ASP.NET, Visual Studio, Dev
Posted:6 Apr 2007
Updated:24 Jun 2008
Views:41,201
Bookmarked:25 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
27 votes for this article.
Popularity: 3.15 Rating: 2.20 out of 5
13 votes, 48.1%
1
5 votes, 18.5%
2
2 votes, 7.4%
3

4
7 votes, 25.9%
5

Introduction

This is a simple code which can be used to change page themes at runtime.

Background

Basic knowledge of stylesheets is required.

Using the code

Live Demo

Before we proceed further I would recommend you to see live demo here to get idea what we are talking about.

Challenges

At first though we may say we can easily achieve this by coding it in Page_Preinit Event as shown below.

protected void Page_PreInit(object sender, EventArgs e)
    {
       Page.Theme = "Black"
       
    }

But problem with this is we cant assign value from dropdown box because Page_Preinit event is fired much before dropdown has changed value.

Solution

  1. Create one session variable which will hold current theme value
  2. On selection change event of dropdown combo box , assign value form combo box to session variable.
  3. During Page_preInit Event assign this variable value to Page.Theme property.
  4. Stop page loading and reload same page again using server.transfer method as shown below
protected void Page_PreInit(object sender, EventArgs e)
    {
        string thm;
        thm = (string)Session["themeSelected"];
        if (thm != null)
        {
            Page.Theme = thm;
            DropDownList1.Text = thm;
        }
        else
        {
            Session["themeSelected"] = DropDownList1.Text;
            Page.Theme = "Blue";
        }

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["themeSelected"] = DropDownList1.Text;
        Server.Transfer(Request.FilePath);

    }

 

Summary

Problem was solved by using session variable and refreshing page at page load event by server.transfer method

Download

Fully functional source code with example can be downloaded from here

Points of Interest

  • Page_preinit is fired much before control initialization
  • Session variable used to store and retrieve page theme at page load event
  • server.transfer play important role here

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rajesh Naik Ponda Goa


Member
RN Softech is a goan software development company, based at Goa, India.
Occupation: Web Developer
Company: www.rnsoftech.com
Location: India India

Other popular HTML / CSS articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 9 of 9 (Total in Forum: 9) (Refresh)FirstPrevNext
GeneralRE: Master Pages with "How to change page theme in asp.net 2.0 dynamically at runtime" PinmemberBrian Fay9:51 4 Nov '09  
GeneralMy vote of 1 Pinmemberykorotia15:39 26 Jan '09  
GeneralWhat about Changing pageLayout at runtime? Pinmemberdevnet2477:07 5 Jun '08  
GeneralRe: What about Changing pageLayout at runtime? PinmemberRajesh Naik Ponda Goa20:07 5 Jun '08  
GeneralRe: What about Changing pageLayout at runtime? Pinmemberdevnet24723:00 5 Jun '08  
GeneralRe: What about Changing pageLayout at runtime? PinmemberRajesh Naik Ponda Goa2:47 9 Jun '08  
GeneralLive Demo not Working PinmemberRanjan.D18:23 21 Apr '08  
GeneralRe: Live Demo not Working PinmemberRajesh Naik Ponda Goa5:05 5 Jun '08  
GeneralAre you serious PinsupporterMark Nischalke12:14 6 Apr '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Jun 2008
Editor:
Copyright 2007 by Rajesh Naik Ponda Goa
Everything else Copyright © CodeProject, 1999-2009
Web10 | Advertise on the Code Project