Placing a Date Last Updated for a Page Inside a User Control...
By: John Kilgo Date: February 17, 2003 Download the code.

You may have noticed that dotnetjohn.com pages now include a "Last Updated:" line at the bottom of all pages. I make extensive use of user controls on this site. The top of the page down through the "Powered by ASP.Net" graphic is a user control. The left side navigation is a user control as is the footer at the bottom of the page. The nice thing about user controls is that you can change the appearance of many pages by making the change in only one file.

When I decided I wanted to show the date last modified at the bottom of my pages, I had only to add the code to my footer.ascx. I was at first concerned that it would show the date last modified for the footer, which was not what I wanted. The page object came to the rescue however.

If you are not using user controls, you probably should be. In the long run they save you a great deal of time. In case you have not used user controls let us spend a minute designing and incorporating a simple one. We'll start with a very simple header control. You need to create a new text file with an extension of .ascx. Copy the following code:

<%@ Control Language="vb" %>
<table width="90%" align="center">
  <tr>
    <td bgcolor="Coral" align="center">This is my Example Header Control</td>
  </tr>
<table>
<p><p>

That's all there is to it. You now have a simple header user control.

We will now create a slightly more complicated footer user control. It is more complicated only by virtue of including our date last modified (updated) code in it. The code is shown below. We have included some code in between <Script> tags. We first find the physical path to the page (which will be the page containing the footer control, not the footer control itself). Using the physical path we then use GetLastWriteTime to get gather the date and time of last modification (write). We then format the date time into the string format we want to use. Near the bottom of the control we write out the string containing our date and time of last modification.

<%@ Control Language="vb" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">
Dim strMod As String

Sub Page_Load(sender As System.object , e As System.EventArgs)
  Dim strPath As String = Page.Request.PhysicalPath
  Dim dteMod As DateTime = File.GetLastWriteTime(strPath)
  strMod = "Last Updated: " & dteMod.ToString("D")
End Sub
</script>

<table width="90%" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#EEEEEE" align="center">This is my Example Footer Control</td>
  </tr>
  <tr><td bgcolor = "#EEEEEE" align="center"><%= strMod %></td></tr>
</table>

We then create a page to contain our two user controls. The code should be self-explanatory. I have colored in blue the pertinent lines of code to incorporate our user controls.

<%@ Page Language="vb" %>
<%@ Register TagPrefix="dnj" TagName="Header" src="ExampleHeader.ascx" %>
<%@ Register TagPrefix="dnj" TagName="Footer" src="ExampleFooter.ascx" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>HeaderFooter</title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<dnj:header id="header1" runat="server" />
<table width="90%" align="center">
  <tr>
  <td align="center"><h1>Hello World!</h1></td>
  </tr>
</table>
<dnj:footer id="footer1" runat="server" />
</body>
</html>

Conclusion: You have just seen how to create and incorporate user controls. The main point of the article was including the date last modified for our containg page inside the footer user control.

You may run the example program here.
You may download the code here.