Click here to Skip to main content
Click here to Skip to main content

Creating custom headers and footers in Application level events using global.asax

, 6 Oct 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
An example of how to create custom headers and footers in Application level events using the global.asax file.

Sample Image - CustomHeaderFooter.jpg

Introduction

This is a very simple example that shows how to use the global.asax file to create custom header and footers for all your pages.

Background

The Global.asax file contains a number of events that happens when any ASP.NET application is running. In this example, I am using the "Application_BeginRequest" and "Application_EndRequest" events to show how to create a custom header and footers. Application_BeginRequest gets fired whenever the ASP.NET page gets a new request to handle. It happens before any page, Web Service, or any HTTP handler gets the opportunity to process the request. Therefore, I am using it to create my custom header here. Application_EndRequest gets fired whenever the request is complete. We can control the application response before handing the event to HTTP handlers. Therefore, I am using it to create my custom footer.

Code

The code is very, very simple. Therefore, I did't bother to include my test application. Basically, do the following steps:

  1. Create a new ASP.NET application using C#.
  2. Visual Studio creates the global.asax file for you.
  3. Replace the code for the Application_EndRequest and Application_StartRequest events with the code below:
  4. protected void Application_BeginRequest(Object sender, EventArgs e)
    {
    
        Response.Write("<H1> Welcome to my website! </H1>" );
        Response.Write(" This is my header that comes from Application level " );
        Response.Write("<HR>");
    
    }
    
    protected void Application_EndRequest(Object sender, EventArgs e)
    {
        int yearDate ;
        string dateStr;
        yearDate = System.DateTime.Now.Year;
        dateStr = yearDate.ToString();
        Response.Write("<HR>");
        Response.Write("Copyright 2002-" + dateStr );
        Response.Write("This is my customer footer that  from Application level" );
        Response.Write("<HR>");
    
    }

That is it. Of course, don't forget to keep the layout of your form to "FlowLayout". Run it, and you will get the custom header and footer as in the image above. However, nothing is stopping you from creating a very fancy header and footer and replacing my code with them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rahman Mahmoodi
Software Developer
Australia Australia
Rahman has worked in different areas of Software System Development Life Cycle. He started working as Tester and then moved to application programming and web application programming.
 
At the moment his area of interest are .Net including ASP.Net/2, VB.net, C# and object oriented design using UML.
 
He has Bachelor of Computing with Distinction Grade.

Comments and Discussions

 
GeneralThank you PinmemberShane Calhoun30-Jun-06 11:00 
Thank you for such an easy example. I hate not finding simple examples of how to do something. This takes 2 minutes to replicate and shows where to begin. I can improve all I want on it, but this gives me a place to start.
Thanks again.
Generalreberbe PinsussAnonymous7-Oct-05 23:31 
GeneralRe: reberbe PinmemberRahman Mahmoodi8-Oct-05 16:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.141015.1 | Last Updated 6 Oct 2005
Article Copyright 2005 by Rahman Mahmoodi
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid