Click here to Skip to main content
15,881,882 members
Articles / Web Development / HTML
Article

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

Rate me:
Please Sign up or sign in to vote.
2.31/5 (30 votes)
24 Jun 2008CPOL1 min read 135K   2.2K   30   12
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.

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)


Written By
Web Developer
India India
I'm a software developer from Goa, India & I take assignments of software development. My specialization is in desktop applications built using latest technologies such as PRISM, WPF, WCF etc

Comments and Discussions

 
Questionbrother i want to change web background with stylesheeet how to mangae stylesheet theme Pin
Abid Hussain-(Abid)10-May-13 6:21
professionalAbid Hussain-(Abid)10-May-13 6:21 
GeneralHere is quiz for the Theme Pin
Jayesh Sorathia23-Jul-12 23:28
Jayesh Sorathia23-Jul-12 23:28 
This is a very good article.
Here is a quiz for the Theme topic.

Quiz : Which Event you are using for dynamically assign Theme to a page ?

Click this link to give your answer. Click Here...
GeneralRE: Master Pages with "How to change page theme in asp.net 2.0 dynamically at runtime" Pin
Brian Fay4-Nov-09 8:51
Brian Fay4-Nov-09 8:51 
GeneralRe: RE: Master Pages with "How to change page theme in asp.net 2.0 dynamically at runtime" Pin
a2zzzz15-Jan-11 20:54
a2zzzz15-Jan-11 20:54 
GeneralMy vote of 1 Pin
noname-201326-Jan-09 14:39
noname-201326-Jan-09 14:39 
QuestionWhat about Changing pageLayout at runtime? Pin
devnet2475-Jun-08 6:07
devnet2475-Jun-08 6:07 
AnswerRe: What about Changing pageLayout at runtime? Pin
Rajesh Naik Ponda Goa5-Jun-08 19:07
Rajesh Naik Ponda Goa5-Jun-08 19:07 
GeneralRe: What about Changing pageLayout at runtime? Pin
devnet2475-Jun-08 22:00
devnet2475-Jun-08 22:00 
GeneralRe: What about Changing pageLayout at runtime? Pin
Rajesh Naik Ponda Goa9-Jun-08 1:47
Rajesh Naik Ponda Goa9-Jun-08 1:47 
GeneralLive Demo not Working Pin
Ranjan.D21-Apr-08 17:23
professionalRanjan.D21-Apr-08 17:23 
GeneralRe: Live Demo not Working Pin
Rajesh Naik Ponda Goa5-Jun-08 4:05
Rajesh Naik Ponda Goa5-Jun-08 4:05 
GeneralAre you serious Pin
Not Active6-Apr-07 11:14
mentorNot Active6-Apr-07 11:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.