RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB.NET
Expertise: Advanced
May 12, 2009

Show a Footer for All ASP .NET Pages in a Site

This tip is useful for displaying a uniform footer in all your site's pages, including any pages served from the site's subdirectories.

In this case, assume you want to display a footer message with each and every request under any subfolder of the site. Consider how shared web-hosting companies display footer banner ads on every web page they serve by automatically appending the banner ad to every page. Note that their developers don't have to write or include any code for that to happen. You want to give your application the exact same capability. Here's the process.

First, add a new Global Application Class (Global.asax) to your application. When you open that file, if you're using Visual Studio, you'll find that the IDE generates commonly used Application and Session event handlers for you. However, the automatically generated event handlers aren't suitable for adding a footer. Application_Start and Application_End won't work, because you want the footer to show on each and every request, not only when the application is starting or ending. Application_Error is obviously to tap into unhandled exceptions at the page level, so that's not appropriate either.

Instead, you need to add an event handler that Visual Studio doesn't insert automatically Application_EndRequest.

First, here's the code:

Sub Application_EndRequest(ByVal sender As Object, ByVal e As EventArgs) 
   With Response
     .Write(String.Format("<div width='{0}' style='{1}'>", _
        Unit.Percentage(100), "text-align:center;"))
     .Write(String.Format("<img src='{0}' alt='{1}'/>", _
        "footer2.jpg", "Footer"))
   End With 
End Sub

Application_EndRequest, as the name suggests, is the last event where you have an opportunity to modify what ASP.NET is sending to the client.

The code adds an image tag and sets its source (src) property to the name of the image to display.

There are other ways to achieve similar functionality, such as using Master Pages or creating a base page class that includes the footer, and inheriting all pages from that; however, this method works even with existing sites.

Rafay Ali
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date