HowTo: Set the Theme for a MasterPage (from code)

The MasterPage doesn't have a property for setting the Theme at design time. Despite this, I wanted to set the Theme for a MasterPage, so I decided to set it from code. I was aware of the possibility to set the Theme through the web.config, but that wasn't the way I wanted to set it. One of the reasons being it would result in the theme being applied to the entire website.

I tried to use the Page_Load, but that resulted in the error "The 'Theme' property can only be set in or before the 'Page_PreInit' event.". That sounds logical, because the Theme makes the Page render in a specific way. So I added a Page_PreInit method with the right parameters, but that didn't do anything. As it turns out, the Master Page doesn't have a Page_PreInit...

To be able to set the Theme for a MasterPage from code, follow these steps:

  1. Add a class with the name ThemeModule (or any other name)
  2. Let the class inherit from IHttpModule
  3. Implement the init method as follows:

    public void Init(HttpApplication context)
    {
        context.PreRequestHandlerExecute += HandlePreRequest;
    } 
  4. void HandlePreRequest (object sender, EventArgs e)
    {
        Page page = HttpContext.Current.CurrentHandler as Page;
        if (page != null)
        {
            page.PreInit += delegate
                                {
                                    page.Theme = DetermineTheme();
                                };
        }
    }

  5. Add the HttpModule to your web application through the web.config:

     
    <
    httpModules>
        <add name="ThemeModule" type="Howtos.ThemeModule"/>
      </httpModules>

That's it! Hope this helps.


A week ago I came up with this solution together with a good friend of mine. The code was on his machine, so he said I had to add a 'thanks to' in this post. Well, here it is: thanks 2 Sander van Kemenade! ;)

Published Sat, Oct 20 2007 11:19 AM by Rick van den Bosch
Filed under: ,

Comments

# re: HowTo: Set the Theme for a MasterPage (from code)

Hello ,good post

but I don't know how to use this ??

I made a class like your steps but

error faced me =>'ThemeModule' does not implement interface member 'System.Web.IHttpModule.Dispose()'

so how to solve this

also after building the class , what is next

sorry I'm new in that. thank you very much

Friday, March 21, 2008 7:58 PM by Gersy

# re: HowTo: Set the Theme for a MasterPage (from code)

Hey there,

Add this:

   public void Dispose()

        {

        }

Thursday, March 27, 2008 2:57 AM by John Edwards

# re: HowTo: Set the Theme for a MasterPage (from code)

hey there,

i add the method dispose() but the error msg is that the system can not load type="Howtos.ThemeModule"

Sunday, April 06, 2008 11:55 PM by Saber

# re: HowTo: Set the Theme for a MasterPage (from code)

Your solution does exactly what you didn't want it to in the first place i.e.: it sets theme for each page in the whole web site.

Sunday, June 15, 2008 2:16 PM by Michal Talaga

# re: HowTo: Set the Theme for a MasterPage (from code)

Hi Michal,

You might be misinterpreting me here, or maybe I didn't explain clearly. Let me try to clear things up :D

All I said was 'One of the reasons being it would result in the theme being applied to the entire website.'

You assume DetermineTheme returns the same theme every time. There might be several themes for my website which should be chosen based on the location of the page, the color of your shirt or the temperature outside. The method might also return null in some specific scenarios, resulting in no theme being applied at all.

This solution is pretty different from setting the theme from the web.config. Or at least I think so... ;)

Kind regards, Rick

Monday, June 16, 2008 10:58 AM by Rick van den Bosch

# re: HowTo: Set the Theme for a MasterPage (from code)

Nice post I'm facing the same problem I will try this now.

Thanks

Satalaj

Wednesday, December 31, 2008 11:28 AM by satalaj

# re: HowTo: Set the Theme for a MasterPage (from code)

Any help on applying this in vb?

Monday, February 09, 2009 7:42 PM by Matt

# re: HowTo: Set the Theme for a MasterPage (from code)

Could you show some ways to apply the DetermineTheme method? Would you suggest cookies, session variables, etc?

Monday, February 09, 2009 8:44 PM by TheModulator

# re: HowTo: Set the Theme for a MasterPage (from code)

Thanks, this is what i was looking for :)

Wednesday, February 11, 2009 10:07 PM by Dragon Ace

# re: HowTo: Set the Theme for a MasterPage (from code)

Rick,

I've just read through your solution, although I haven't tried implementing it yet, and I like it.

However, it occurs to me that the title " HowTo: Set the Theme for a MasterPage" is actually a misnomer since your actually just appending to all Pages' PreInit events - whether they use a MasterPage or not.  Perhaps a better title would be in order to catch those developers looking for this type of solution but not necessarily using a MasterPage.

Wednesday, April 22, 2009 10:18 AM by Simon Pickersgill

# re: HowTo: Set the Theme for a MasterPage (from code)

I am using a dropdown list on a master page then how can i bind this dropdown list with themes.If user select the theme from the dropdown list that theme should be applied on whole page...what is the body of this DetermineTheme() method

Friday, May 22, 2009 10:07 AM by archana

# re: HowTo: Set the Theme for a MasterPage (from code)

Hey, I love this code and I'm trying to use it (over using something like a base-page where I'd have to change the base-page for all of my aspx pages)

I'm stuck on trying to get the user's ID inside the "DetermineTheme" function. That way I can do a database lookup on some tables and see what theme they should have. Right now I'm using "Membership.GetUser(User.Identity.Name).ProviderUserKey.ToString()"

which doesn't seem to work.

Says "User" does not exist inside the current context.

Any thoughts?

Monday, May 25, 2009 3:21 AM by Wunder

# re: HowTo: Set the Theme for a MasterPage (from code)

Wunder - try HttpContext.Current.User...

Thursday, May 28, 2009 3:13 PM by Brian

# re: HowTo: Set the Theme for a MasterPage (from code)

Archana - re: enumerating themes for a drop down...

Try:

DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/App_Themes/"));

foreach(Directory d in di.GetDirectories())

  add the theme to the drop down

Thursday, May 28, 2009 3:16 PM by Brian

# re: HowTo: Set the Theme for a MasterPage (from code)

Hi, Good post. Do you have any sample code.

Monday, July 06, 2009 3:04 PM by Vijay Jadhav

# re: HowTo: Set the Theme for a MasterPage (from code)

Brilliant!

Exactly what I was looking for. Thank you!

Wednesday, November 25, 2009 12:45 AM by Jonny Augustsson

# re: HowTo: Set the Theme for a MasterPage (from code)

----> same problem as saber

hey there,

i add the method dispose() but the error msg is that the system can not load type="Howtos.ThemeModule"

Wednesday, February 17, 2010 9:03 AM by Stanley S.

# re: HowTo: Set the Theme for a MasterPage (from code)

Sure do love how people put code out there and do not give you all of it.  

<system.webServer>

 <modules>

   <add name="ThemeModule" type="Howtos.ThemeModule"/>

 </modules>

</system.webServer>

line is need here too.

still doe snot work for me but I am still trying

Saturday, April 24, 2010 10:52 PM by Lawrence Thurman

# re: HowTo: Set the Theme for a MasterPage (from code)

Or even simpler for many you can stick it into the global.asax.

   protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e) {

           Page page = HttpContext.Current.CurrentHandler as Page;

           if (page != null) {

               page.PreInit += delegate {

                   page.Theme = DetermineTheme();

               };

           }

   }  

Thursday, June 10, 2010 1:11 AM by TZAdvantage

# re: HowTo: Set the Theme for a MasterPage (from code)

Hey Thanks TZAdvantage, your approch is much simpler. you r awesome. :)

Saturday, February 26, 2011 12:19 PM by Swapnil

# re: HowTo: Set the Theme for a MasterPage (from code)

<system.webServer>

<modules>

  <add name="ThemeModule" type="Howtos.ThemeModule"/>

</modules>

</system.webServer>

Actualy u need to change the Howtos.ThemeModule to the class namespace eg. My apllication has just the ThemeModule class (outside any namespace) then i just used

<add name="ThemeModule" type="ThemeModule"/

Tuesday, August 23, 2011 7:36 PM by Re

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 3 and 3 and type the answer here: