Pages

Thursday, November 18, 2010

Theme Managment with httpHandlers


Theme Managment with httpHandlers , How to do that? 


You can do this in a module:
  public class ThemeModule : IHttpModule
  {
    public void Init(HttpApplication context)
    {
      context.PreRequestHandlerExecute += new EventHandler(this.app_PreRequestHandlerExecute);
    }

    private void app_PreRequestHandlerExecute(object Sender, EventArgs E)
    {
      Page p = HttpContext.Current.Handler as Page;
      if (p != null)
      {
          string strTheme = "Theme1";
            if (Convert.ToString(HttpContext.Current.Session["THEME"]) != string.Empty)
                strTheme = Convert.ToString(HttpContext.Current.Session["THEME"]);
            return strTheme;
          p.StylsheetTheme;
      }
    }

    public void Dispose()
    {
    }
  }
   Then add the module in config:
   
     
   
 

An alternative however, is to use an even in global.asax:
private void app_PreRequestHandlerExecute(object Sender, EventArgs E)
    {
        Page p = HttpContext.Current.Handler as Page;
      
        if (p != null)
        {
            string strTheme = "Theme1";
            if (Convert.ToString(HttpContext.Current.Session["THEME"]) != string.Empty)
                strTheme = Convert.ToString(HttpContext.Current.Session["THEME"]);
            p.StyleSheetTheme = strTheme;
        }
    } This saves having to configure the module. The event runs early enough int he page lifecycle to allow the theme to be set.

No comments:

Post a Comment