Dynamic Master Pages

by Juan 18. July 2008 15:16

I wanted to create a template web site with the ability to completely change its layout via the web.config file, and it was also a good excuse to look into HttpModules.

HttpModules are services that execute when the requests reach the webserver, you can use them to do whatever you want... like compressing the output before sending it to the client, parsing scripts and put them all in a single file, etc, etc, etc

This time, what I want to do is set the the page's MasterPage according to a value in the web.config file.

What I do is register for the page's PreInit method, and set the MasterPageFile property in the handler, like this:

public void Init(HttpApplication context)

{       

    context.PreRequestHandlerExecute += new EventHandler(context_PreRequestHandlerExecute);   

}   

 

void context_PreRequestHandlerExecute(object sender, EventArgs e)   

{       

    Page page = HttpContext.Current.CurrentHandler as Page;       

    if (page != null)       

        page.PreInit += new EventHandler(page_PreInit);       

}   

 

void page_PreInit(object sender, EventArgs e)   

{       

    Page page = sender as Page;       

    if (page != null)

        page.MasterPageFile = "~/MasterPages/" + ConfigurationManager.AppSettings["MasterPageTheme"] + "/site.master";       

}

In that example, you have to have a MaterPageTheme setting with the name of the folder that contains site.master.

image

Then you just need to register the module, and you are good to go

<httpModules>

    <add name="MasterPageModule" type="MasterPageModule"/>
</httpModules>

You can download the code below, just drop it in the App_Code folder

MasterPageModule.zip (620.00 bytes)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , ,

Programming

Powered by BlogEngine.NET 1.4.5.7
Theme by Mads Kristensen Modified by Juan Manuel Formoso

About the Author

Juan Manuel
Networking
View my LinkedIn profile View my Technorati profile View my Facebook profile View my bookshelf
View my Plaxo profile View my digg profile

Juan Manuel Formoso
There is a theory which states that if ever anyone discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something more bizarrely inexplicable.

There is another theory which states that this has already happened.

E-mail me Send mail

Ads

Most comments

Cristian Pereyra Cristian Pereyra
3 comments
ar Argentina
Miguel Miguel
2 comments
us United States
Ariel Ariel
1 comments
ar Argentina
Mauro Mauro
1 comments
ar Argentina
Daniel Daniel
1 comments
mx Mexico

Google Reader Picks

Calendar

<<  July 2008  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Recent posts

Recent comments

Comment RSS

Disclaimer

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

© Copyright 2008
XFN Friendly