views:

277

answers:

2

I want to be able to set an ASP.NET Theme in a gateway page, which will determine the theme based upon a partner id or something like that. I figure I'd just stick the theme name in session and get it from there if I need it.

I'm having trouble finding the right place to put this code in a single place.

I cannot seem to see a global place you can set Theme. You have to set it for each page.

As described in MSDN you can assign the Theme property in the PreInit function for a page.

Protected void Page_PreInit(object sender, EventArgs e)
{
    switch (Request.QueryString["theme"])
    {
        case "Blue":
           Page.Theme = "BlueTheme";
            break;

        case "Pink":
           Page.Theme = "PinkTheme";
            break;
    }
}

So i thought ok - i'll just do that in my master page. Unfortunately copying this exact same code into a master page doesn't work. So I thought - hmm maybe master pages dont use this event. It turns out this is true.

I REALLY dont want to have to put this theme code on every content page. That seems completely stupid. But I cannot yet find anopther way. MSDN describes only two ways to apply a theme to a page -- in the web.config or with Page.Theme.

Am i going to have to create a subclass of Page and have all my pages subclass that page, and override PreInit in that subclass? I think I must be missing something becasue I cant imagine MS really expects people to set Theme programatically on every content page.

+3  A: 

One option would be to create your own Page base class and handle the theme switching/setting there. Then use that class as the base class of all your pages.

public class PageBase : Page
{
  protected void Page_PreInit(object sender, EventArgs e)
  {
    //..
    Page.Theme = "BlueTheme";
    //..
  }
}


public class MyPageOne : PageBase
{
 ...
}
M4N
A: 

If your site uses clean markup you could just as easily switch style sheets based on the querystring, browser type, login info etc.

Using CSS could save you alot of time in the future over creating new themes/masterpages/markup... etc.

Birk
my theme is also being used to drive other logic, such as which buttons appear in menu bars and some other dyanmic controls on pages
Simon_Weaver