How to display the Last Modification Date and Time for ASP.NET web page?

 Copyright © 2007-2012 www.AspDotNetFaq.com

Here is a simple method that you can put into your ASP.NET page code behind file
that can retrieve Last Modification DateTime:

    public DateTime GetLastModificationDateTime()

    {

        string currentPageFilename = Context.Request.PhysicalPath;

        if (System.IO.File.Exists(currentPageFilename))

        {

            return System.IO.File.GetLastWriteTime(currentPageFilename);

        }

        else

            return DateTime.Now;

    }


and here is how you can show that information on your page:

    <html xmlns="http://www.w3.org/1999/xhtml">

        <head runat="server">

            <title>Untitled Page</title>

        </head>

        <body>

            <form id="form1" runat="server">

                <div>

                    Last Modified Date Time: <%= GetLastModificationDateTime().ToString("dd-mmm-yyyy HH:mm:ss") %>

                </div>

            </form>

        </body>

    </html>


The best approach is to create a custom base Page class, for example you can call it SmartPage and put this code
in that base Page class.
Then for each Web Page in your website use that custom page class as ancestor.

Here is an example how your base page code could look like:

File: App_Code/SmartPage.cs:

/// <summary>

/// Summary description for SmartPage

/// </summary>

public class SmartPage : System.Web.UI.Page

{

    public SmartPage()

    {

        //

        // TODO: Add constructor logic here

        //

    }

 

    public DateTime GetLastModificationDateTime()

    {

        string currentPageFilename = Context.Request.PhysicalPath;

        if (System.IO.File.Exists(currentPageFilename))

        {

            return System.IO.File.GetLastWriteTime(currentPageFilename);

        }

        else

            return DateTime.Now;

    }   

 

}


and here is how would your Web Page look like, when inherited from your SmartPage base class:


public partial class LastPageModificationDate : SmartPage

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

    }

}


This way, you can call the inherited GetLastModificationDateTime() method in every web page without having to Copy/Paste
the method declaration to each page instance on your WebSite.

After all, what do we have Inheritance for? 



 Copyright © 2007-2012 www.AspDotNetFaq.com