LolaTheriotDynamically Setting the Theme

Themes can be used to customize the look of your Website. If you need to select the theme based on user settings, you’ll be glad to know that ASP.NET allows you to set the theme of a page dynamically when the page is being created. This process is pretty straight forward; however, there are a couple of issues that may arise.

First of all, ASP.NET establishes the current theme prior to the page’s Load() event. In order to dynamically change the theme before the Load() event, you’ll need to set it during the page’s PreInit() event.

Setting the Theme in the PreInit() Event

That was easy enough. But what happens if your site uses a master page? Master pages allow you to control the appearance of all the pages on your site from a single master page. So it makes sense to want to set the theme from the master page. However, master pages do not provide a PreInit() event. So setting the theme dynamically from a master page is not as easy as you might guess.

One way to deal with this is to create a class that derives from IHttpModule. This class can be made to respond to all page requests on your site, and it gets called just before the request is handled, in plenty of time to set the theme for the current page.

public class ThemeManager : IHttpModule
{
   public ThemeManager()
    {
    }
  public void Init(HttpApplication app)
    {
        // Set our handler to be called just before handling a request
        app.PreRequestHandlerExecute +=
            new EventHandler(Context_PreRequestHandlerExecute);
    }
  void Context_PreRequestHandlerExecute(object sender, EventArgs e)
    {
        // Note: If handler does not implement IRequiresSessionState or
        // IReadOnlySessionState then Session state will be unavailable
        if (HttpContext.Current.CurrentHandler is Page)
        {
            // Set theme
            Page page = (Page)HttpContext.Current.CurrentHandler;
            page.Theme = "MyTheme";
        }
        public void Dispose()
        {
        }
    }
}

Setting the Theme from an IHttpModule-Derived Class

In order to use this class, you’ll have to modify your web.config file.

<configuration>
 <system.web>
 <httpModules>
 <add />
 </httpModules>
 </system.web>
</configuration>

Web.config Changes

Also, be sure to clear any theme settings from your web.config file. Otherwise, it’s possible for ASP.NET to get confused and possibly reference multiple CSS files.

ASP.NET themes are quite powerful, and can be used to manage the appearance of various controls and images. Another thing themes do is let you specify CSS, or style sheets. If this is all your themes are used for, then a much simpler approach can be taken.

All you need to do is add a Literal control inside of the <head> section of your master page. Then, you can set it to include your style sheet during the master page’s Load event.

Since you aren’t modifying the look of controls using skins, there is no need to change this setting in the PreInit() handler. In fact, you could handle it even later than the Load event since all that needs to happen is your page references the correct CSS file.

http://www.all1tunes.com

http://www.all1social.com

Tags: , , , , , , , ,