.NET Tips and Tricks

Blog archive

Manage Pages Across the Web Site from One Location

Sometimes there is some processing that needs to be added to every page on the site. For instance, I had a client who wanted to add code to every WebForm's PreInit event to set Page's Theme property dynamically. Changing every PreInit event in every page sounded like the definition of no fun at all.

But there's a better solution. One place to put code required on every page is in an HttpModule. HttpModules process every page request that comes into the site. You can create your own HttpModule so that, when a request for a page comes to the site, you can add to code to any event on the page.

The first step is to create a class that implements the IHttpModule interface:

Imports System
Imports System.Web
Imports System.Web.UI

Public Class ManageTheme
  Implements IHttpModule
In the class' Init method, tie a method of your own to the PreRequestHandlerExecute event that's fired by ASP.NET before processing a page. You'll use your method to customize the page's processing. You can get to the PreRequestHandlerExecute event through the HttpApplication object passed to the Init method:
 Public Sub Init(Context As HttpApplication)_
            Implements IHttpModule.Init
         AddHandler Context.PreRequestHandlerExecute, 
                     AddressOf WireUpPreInitEvent

  End Sub
Your method will now be called whenever a page is processed. In your method, you can add code to any of the page's events. This code ties a method called sitePreInit to the Page's PreInit event:
 Sub WireUpPreInitEvent(sender As Object, e As EventArgs)
   If TypeOf HttpContext.Current.CurrentHandler Is Page Then
     Dim p As Page
     p = CType(HttpContext.Current.CurrentHandler, Page)
     If p IsNot Nothing Then
       AddHandler p.PreInit, AddressOf SitePreInit
     End If
   End If
 End Sub
Finally, you can add the code you want to run on every page on your site to the event you've wired up:
  Sub SitePreInit(sender As Object, e As EventArgs)
    If TypeOf sender Is Page Then
      Dim p As Page
      p = CType(sender, Page)
      If p IsNot  Nothing Then
        p.Theme = "MyTheme"
      End If
    End If
  End Sub

Public Sub Dispose() _
  Implements System.Web.IHttpModule.Dispose

End Sub

The last step is to update the httpModules element to incorporate your module into your site's processing pipeline:

  <add name="MyHttpModule" type="ManageTheme"/>

Posted by Peter Vogel on 08/12/2011 at 1:16 PM

comments powered by Disqus


  • What's New in Visual Studio 2019 v16.5 Preview 2

    The second preview of Visual Studio 2019 v16.5 has arrived with improvements across the flagship IDE, including the core experience and different development areas such as C++, Python, web, mobile and so on.

  • C# Shows Strong in Tech Skills Reports

    Microsoft's C# programming language continues to show strong in tech industry skills reports, with the most recent examples coming from a skills testing company and a training company.

  • Color Shards

    Sharing Data and Splitting Components in Blazor

    ASP.NET Core Version 3.1 has at least two major changes that you'll want to take advantage of. Well, Peter thinks you will. Depending on your background, your response to one of them may be a resounding “meh.”

  • Architecture Small Graphic

    Microsoft Ships Preview SDK, Guidance for New Dual-Screen Mobile Era

    Microsoft announced a new SDK and developer guidance for dealing with the new dual-screen mobile era, ushered in by the advent of ultra-portable devices such as the Surface Duo.

  • How to Create a Machine Learning Decision Tree Classifier Using C#

    After earlier explaining how to compute disorder and split data in his exploration of machine learning decision tree classifiers, resident data scientist Dr. James McCaffrey of Microsoft Research now shows how to use the splitting and disorder code to create a working decision tree classifier.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events