Rick van den Bosch - Blog

... on .NET, software architecture, software development and whatnot

Recent Posts

Tags

News

  • Live space

    Photo blog

    Follow me at twitter

    Rick  van den Bosch

    LinkedIn profile

    Add to Technorati Favorites

    Disclaimer
    The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Community

Email Notifications

Blogs I read

Interesting links

Archives

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! ;)

Comments

Gersy said:

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

# March 21, 2008 7:58 PM

John Edwards said:

Hey there,

Add this:

   public void Dispose()

        {

        }

# March 27, 2008 2:57 AM

Saber said:

hey there,

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

# April 6, 2008 11:55 PM

Michal Talaga said:

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.

# June 15, 2008 2:16 PM

Rick van den Bosch said:

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

# June 16, 2008 10:58 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)