Taking Care Of Pre_Init

The Page class exposes a Pre_Init event because the MasterPageFile and Theme properties need to be set early in the lifecycle of a web form. Pre_Init is the event to hook if you want to assign these properties dynamically in code. The natural question is how to implement master page and theme selection for all web forms in an application without adding a Pre_Init event handler to every single web form.

Brock Allen has one elegant solution, which is to use an HttpModule. Brock’s module hooks the PreRequestExecuteHandler and ultimately handles all the PreInit events with a single method in an HttpModule. I haven’t touch VB in a week, so here is what that solution would look like in VB:

Imports System.Web

Public Class MasterAndThemeModule
    
Implements IHttpModule

    
Public Sub Dispose() Implements IHttpModule.Dispose
    
End Sub

    Public Sub Init(ByVal context As System.Web.HttpApplication) _
            
Implements IHttpModule.Init

        
AddHandler context.PreRequestHandlerExecute, _
                  
AddressOf Application_PreRequestHandlerExecute
    
End Sub

    Public Sub Application_PreRequestHandlerExecute(ByVal sender As Object, _
                                                    
ByVal e As EventArgs)
        
Dim application As HttpApplication
        application =
DirectCast(sender, HttpApplication)

        
Dim page As Page
        page =
TryCast(application.Context.CurrentHandler, Page)

        
If Not page Is Nothing Then

            AddHandler page.PreInit, _
                    
AddressOf Page_PreInit
        
End If
    End Sub

    Public Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)

        
Dim page As Page
        page =
DirectCast(sender, Page)
        page.MasterPageFile =
"code to select master page"

    End Sub

End
Class

You could drop the above code into an App_Code file and configure the module in web.config like so:

<httpModules>
   <
add name="MasterAndThemeSelection" type="MasterAndThemeModule"/>
</
httpModules>

Of course, the beauty of an HttpModule is that you could put the code into a class library and configure the module to run in a number of applications.

The alternative is to use inheritance, which requires a base class derived from the Page class. s

Imports System.Web.UI

Public Class BasePage
    
Inherits Page

    
Public Sub Page_PreInit(ByVal sender As Object, _
                            
ByVal e As EventArgs) Handles Me.PreInit
        
Me.MasterPageFile = "code to select master page file"
    End Sub
End
Class

Any web form that wants to take advantage of the common PreInit event handler will need to inherit from the BasePage class either by changing the Inherits attribute in the @ Page directive (for forms with no CodeFile) or by changing the Inherits line in the CodeFile. Inheritance can be slightly more intrusive, but it’s also easier to opt out of the behavior by not deriving from the BasePage.

posted on Friday, December 09, 2005 10:31 PM by scott

Comments

Friday, December 09, 2005 8:33 PM by Thomas Eyde

# re: Taking Care Of Pre_Init

Thanks for the info. It's not you I am bugging, but I have this question:

Is it possible to package the master page file as a class? We don't really want to pass that look and feel *file* around, do we?

I was really hoping I could compile the master into the assembly and then reuse it in other projects. Seems like I have to wait for that one.
Friday, December 09, 2005 9:11 PM by scott

# re: Taking Care Of Pre_Init

Thomas - I think there are ways to compile pieces (master pages, user controls) and then reuse them. I have not tried it with the RTM bits so I'll need to do a few experiments...
Sunday, December 11, 2005 8:30 PM by Christopher Steen

# Link Listing - December 11, 2005

Caveats using DateTime when building Interoperable Web
Services [Via: Anil
John ]
How to Open a...
Thursday, February 23, 2006 10:57 PM by K. Scott Allen

# Caveat With ASP.NET Precompilation and web.config Settings

When ASP.NET 2.0 compiles a web site, it reads the site’s web.config to pick up a few settings that affect...
Friday, February 24, 2006 5:56 PM by stan

# re: Taking Care Of Pre_Init

I tried to put this in global.asax file like Brock, but got error

'Page' is not a member of 'System.EventArgs'.

Do I put this is Generic Handler or in VB.Class file?

What might I be going wrong?


Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs)
Dim t As String = HttpContext.Current.Profile.GetPropertyValue("Theme")
If t <> "" And t.Length > 0 Then
e.Page.Theme = t
Else
e.page.Theme = "ulaanBlue"
End If

End Sub
Tuesday, August 22, 2006 3:01 PM by anabhra

# re: Taking Care Of Pre_Init

Any futher info on sharing the master pages across applications (in the same domain)?
Wednesday, August 23, 2006 6:45 AM by scott

# re: Taking Care Of Pre_Init

Friday, October 27, 2006 6:37 AM by Ezequiel Jadib

# re: Taking Care Of Pre_Init

I use the httpModule, but i have this error

Content controls have to be top-level controls in a content page or a nested master page that references a master page

how can i do?
Sunday, October 29, 2006 1:36 PM by scott

# re: Taking Care Of Pre_Init

@ Ezequiel: You must be setting the MasterPageFile for an .aspx page that does not use master pages. To set the MasterPageFile property on an .aspx means it can only have <asp:Content> controls inside.
Monday, November 27, 2006 10:41 AM by venjiang

# ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps

ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps
Wednesday, February 14, 2007 7:49 PM by lizhiwen

# ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps (转载)

MasterPagesareagreatadditiontotheASP.NET2.0featureset,butarenotwithouttheirquirks.T...
Thursday, February 15, 2007 6:06 PM by lizhiwen

# ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps (转载) ——母版页的一些技巧和窍门

这篇文章是在网上找到的,我感觉针对MasterPage写得挺不错,所以转载了过来。但是本人英语实在是太烂,看这篇文章时花费的时间N长,为了以后看时不那么费劲,于是用自己巨烂的英语瞎翻译了一下。如果谁觉...
Saturday, April 21, 2007 12:53 AM by High_Mount

# ASP.NET2.0 母版页使用

ASP.NET 之父scott写的关于母版页使用的文章,很有参考价值!
Thursday, April 26, 2007 9:36 PM by szw104

# ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps

ASP.Net2.0-MasterPages:Tips,Tricks,andTrapsPostedbyscotton2006年4月11日MasterPagesarea...
Thursday, July 31, 2008 9:03 PM by WCF群组博客

# ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps

ASP.Net2.0-MasterPages:Tips,Tricks,andTraps PostedbyscottonT...
Wednesday, January 21, 2009 1:40 AM by 海洋——海纳百川,有容乃大.

# ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps -- FAQ

Masterpages areagreatadditiontotheASP.NET2.0
Wednesday, January 21, 2009 1:58 AM by 海洋——海纳百川,有容乃大.

# ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps -- FAQ

Masterpages areagreatadditiontotheASP.NET2.0
Wednesday, January 28, 2009 12:53 AM by 海洋——海纳百川,有容乃大.

# ASP.Net 2.0 - Master Pages: Tips, Tricks, and Traps -- FAQ

MasterpagesareagreatadditiontotheASP.NET2.0