devxlogo

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

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("
", _ Unit.Percentage(100), "text-align:center;")) .Write(String.Format("{1}", _ "footer2.jpg", "Footer")) .Write("
") 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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist