If you’ve been following this Blog you probably know I’ve been mucking around with a bunch of infrastructure stuff recently and one of the things I’ve played with a lot is trying to build a generic solution for skinnable UIs. To that effect I ended up using a mixture of Themes and Master Pages in a separate set of template directories that match the Themes templates.
The idea if you change the Theme the Master Page template changes along with the theme. I rigged this up a while ago but it was a bitch to get this to work right in ASP.NET 2.0 stock projects because of various inheritance issues. In the end I got it to work though although I had to make some compromises in what goes onto the master pages (it works fine in WAP though).
Today I finally put that stuff live on my site, and just for kicks I decided to try and switch themes on the pre-compiled site. To my dismay – nothing happened. I changed the theme in the web.config file, but it no effect at all on the site... What? Isn’t that the whole point of Themes?
Back here in my dev environment it works fine, so it must have something to do with precompilation. I double checked by compiling my app into a local deployment directory and then setting up a virtual for it and sure enough I see the same behavior – the app is stuck with whatever Theme was used in web.config when the project was compiled and changing the value in Web.config has zero effect. Chalk that one up to another unexpected ASP.NET 2.0 project behavior where design time and runtime vary.
This project is a stock project compiled with fixed file options, non-updateable and then set up using ASPNET_MERGE into a single assembly. What’s really odd is that the web.config setting that is during compile time seems to determine which Theme the app is stuck on. I haven’t had the inclination to check this out further, but this seems pretty silly if theming adjustments through web.config doesn’t work with a precompiled site somehow. I must be missing something here...
I didn’t bother digging deeper because I wanted to add dynamic application driven theming to the site anyway, and so the web.config setting was just a temporary solution. In the end I wanted it so that Themes can be interactively changed through the Configuration API of the application. Thankfully setting the Theme through Page.OnPreInit works correctly and that's what I'm doing now.
In order to select set the theme (which sets both the Theme as well as the Master Page template) at runtime I use code like this:
protected override void OnPreInit(EventArgs e)
this.Theme = App.Configuration.Theme;
// *** Use a templated MasterPage
if (this.TemplateMasterPage != "" && !string.IsNullOrEmpty(this.Theme))
this.MasterPageFile = this.GetThemeTemplatePath(this.TemplateMasterPage);
/// Figures out the appropriate theme specific Template path
/// that is used to hold Master Pages and other templates.
/// This version returns a fully qualified file Url.
/// <param name="FileName"></param>
public string GetThemeTemplatePath(string Filename)
if (Filename == null)
return "~/App_Templates/" + this.Theme + "/" + Filename;
This code is defined in my page base class for the app. It first gets the Active Theme for the application from my Configuration store and then assigns that explicitly which effective changes the theme. The Theme is then used to look up the appropriate Master Page template by filename. Nice and simple and it works effectively.
In the configuration form the Theme is then displayed and selected and stored in the global configuration object. To get a list of themes I found this code from K. Scott Allen.
/// Loads the list of available themes into txtTheme dropdown
private void LoadThemes()
VirtualDirectory ThemeDir = HostingEnvironment.VirtualPathProvider.GetDirectory("~/App_Themes");
foreach (VirtualDirectory Dir in ThemeDir.Directories)
this.txtTheme.Items.Add( Dir.Name );
I had been parsing the directories out of the App_Themes directory and like Scott I was worried about the permissions issues that might not allow access there.
The form displays among other things the list of themes and lets the admin pick the theme that is used on the site.
I’ve got what I needed working but I’m still thinking about this funky behavior where the web.config setting isn’t changing the them... Any body have any thoughts?
Other Posts you might also like