OdeToCode IC Logo

Taking Care Of Pre_Init

Saturday, December 10, 2005

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


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

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

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

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.