You are here: Home > Articles > Article Display

Smart headers and footers using ASP.NET User Controls

Good site usability often means removing links from one page back to itself. In this article we will look at how to create an ASP.NET User Control which will act as a common header to a site. It will automatically know which page we are looking at, and it will remove links to the same page from itself. For example, on this site, if we click on the About us section of the header, it will take you to the page, and it will make that link inactive. That way, we know that we are under that section, and we can't click on it anymore.

Published: Dec 23, 2002
Tested with: ASP.NET 1.1
Category: ASP.NET
33,845 views

Introduction

Good site usability often means removing links from one page back to itself. In this article we will look at how to create an ASP.NET User Control which will act as a common header to a site. It will automatically know which page we are looking at, and it will remove links to the same page from itself.

Include files in classic ASP

One of the nicest things about classic ASP is include files. We could write any page, which could include anything from HTML, Javascript or VBScript, and position it anywhere on any other page by relative or virtual reference. The most common use of include files was to create common headers or footers, that would go on every page on the site. That way, we could edit just one file and it would affect the whole site. Using includes was very easy indeed. For example, to create a header and footer for this site, we could use the following code:

1 <table width="100%" border="0" cellspacing="0" cellpadding="0">
2     <tr>
3         <td>
4             <a href="/default.aspx"><img src="/images/xefteri.gif" width="300" height="65" border="0" alt="HOME -- Xefteri-Articles for Web Developers"></a>
5         </td>
6     </tr>
7     <tr>
8         <td>
9             <a href="/default.aspx">Home</a> |
10             <a href="/articles/default.aspx">All Articles</a> |
11             <a href="/contactus.aspx">Contact us</a> |
12             <a href="/about.aspx">About us</a>
13         </td>
14     </tr>
15 </table>

We could give this file an extension of ASP, INC or even HTM, and then call it from another page like so:

  • Relative: <!--#include file="../../includes/header.asp"-->
  • Virtual: <!--#include virtual="/includes/header.asp"-->

The result would be something like this:

Example of how a header and a footer is used

There are however, some problems with include files:

  1. The IIS Server caches interpreted scripts to increase speed. There is no way though to cache include files on their own. Rather, the page that includes them is cached as a whole. An alternative is to use Server.Execute(filename), but that is not the default use, and it has problems of its own.
  2. Include files are placed in the page before execution occurs. Dynamically positioning include files is possible but requires work-arounds.
  3. Code in the include files can conflict with code on the page, namely variables, if all code is not encapsulated into subs and functions.

Let's take the case of creating a smart common header for a site, like we described above. This is possible, but actually pretty hard to do with classic ASP includes. The steps needed to do this would be something like this:

  • Get the current page
  • Create a conditional statement that will run different pieces of code depending on the current page
  • For each link in the header that we want to turn smart, we would have to check if it refers to the current location. If it is, then we simply do a Response.Write of the text with no link. If each link does not refer to the current location, then we would output the text wrapped in its link.

That's too much repetition.

Include files with ASP.NET: enter User Controls

We can still use include files in ASP.NET in pretty much the same way as classic ASP if we want to. However, User Controls offer greater flexibility, reliability and speed. We'll see this as we create a header User Control for this site.

I am very much in support of good usability on all sites I work on. One of the important elements of a good navigational usability, according to Jacob Nielsen's column, is to not provide links on a page that link to themselves. This simply confuses the users, who might be clicking on a link and coming back to the same page without realizing it. This problem is made even worse by many sites, which implement style sheets that make visited links indistinguishable from non-visited ones. The user is doomed to try links over and over, only to come back to a page they have already visited.

We can turn the above HTML (include file) into a User Control very easily. Let's look at the complete code needed. The file will now be saved as head.ascx.

1 <%@ Control Language="VB" EnableViewState="False" %>
2 <script language="VB" runat="server">
3 Sub Page_Load()
4     Try
5         Dim Referer As String = Request.ServerVariables("SCRIPT_NAME")
6         Select Case Referer
7             Case "/default.aspx"
8                 aHome.Href = ""
9                 aImgHome.Href = ""
10             Case "/articles/default.aspx"
11                 aAllArticles.Href = ""
12             Case "/contactus.aspx"
13                 aContactUs.Href = ""
14             Case "/about.aspx"
15                 aAboutUs.Href = ""
16         End Select
17     Catch
18     End Try
19 End Sub
20 </script>
21 <table width="100%" border="0" cellspacing="0" cellpadding="0">
22     <tr>
23         <td>
24             <a id=" aImgHome" runat="server" href="/default.aspx"><img src="/images/xefteri.gif" width="300" height="65" border="0" alt="HOME -- Xefteri-Articles for Web Developers"></a>
25         </td>
26     </tr>
27     <tr>
28         <td>
29             <a id="aHome" runat="server" href="/default.aspx">Home</a> |
30             <a id="aAllArticles" runat="server" href="/articles/default.aspx">All Articles</a> |
31             <a id="aContactUs" runat="server" href="/contactus.aspx">Contact us</a> |
32             <a id="aAboutUs" runat="server" href="/about.aspx">About us</a>
33         </td>
34     </tr>
35 </table>

Let's explain what is happening here. The Request.ServerVariables("SCRIPT_NAME") gets the virtual path of the current page. We store this in the string variable Referer. For example, the virtual path of this page is /articles/23dec2002/default.aspx.

We then perform a Select...Case condition and turn off the self-referencing links from the page. We can do this easily because we have given each <a href> in our HTML a unique ID and told it to run on the server. To turn off the link for the About us link for example, we code it as aAboutUs.Href = "". Setting the Href property to an empty string, actually removes it.

Adding the User Control on any page

To add this smart header now to any page, we must first register it and then call it:

1 <%@ Page Language="VB" runat="server" %>
2 <%@ Register TagPrefix="UserControl" TagName="Header" Src="/usercontrols/head.ascx" %>
3 <html>
4 <head>
5     <title>Xefteri - Smart headers and footers using ASP.NET User Controls</title>
6 </head>
7
8 <body>
9     <UserControl:Header runat="server" />
10     ...
11 </body>
12 </html>

Conclusion

We have seen how easy it is to create a User Control that acts as a header for our site. With it, we can make it smart enough to eliminate any self-referencing links and thus improve usability on our sites.

 



Other articles in this category
  1. Maintaining Sorting while Paging in an ASP.NET Datagrid
    December 18, 2002
    The Datagrid server control offers much control and flexibility in presenting data. Two of the actions that are hard-wired into it are Paging and Sorting. On their own they work great, but not so well together. When you sort a column and then move to a previous or next page, the sorting preference is not maintained. In this article we will see how to maintain both by using the Viewstate object.
  2. How postback works in ASP.NET
    December 10, 2002
    In this article, we will take a closer look at how ASP.NET pages post back to themselves, and how to customize this feature in our web applications.
  3. Viewing and editing file and directory attributes in ASP.NET
    December 2, 2002
    The System.IO.FileAttributes class gives us access to file/directory attributes. In this article, we'll see how to use this class to first read the current attributes and then change them.
  4. Copying a directory in ASP.NET
    November 25, 2002
    The System.IO.DirectoryInfo class does not come with a method to copy a directory. In this article, we'll see how to create a method to do that, and then use it in an ASP.NET page.
  5. Creating and consuming a Web Service using Visual Studio .NET
    November 18, 2002
    This article will go through the complete process of how to create a Web Service and then how to consume it in any ASPX page. We'll do this entirely within the Visual Studio.NET. Our Web Service will convert Fahrenheit degrees to Celcius.