Code an ASP.net 2.0 link with an automatic updated date

Last post 07-18-2007 9:18 PM by wonjartran. 9 replies.

Sort Posts:

  • Code an ASP.net 2.0 link with an automatic updated date

    07-15-2007, 7:30 PM
    • Member
      111 point Member
    • wonjartran
    • Member since 06-12-2007, 10:19 AM
    • Posts 659

    I am developing an ASP.net 2.0 web site for an existing
    classic VS 6 project.  Just noticed that the URL's of some
    classic ASP pages were shown with ServerName/RootFolderName only.
    Another unique feature of these pages is that the link and
    the date on the ASP page were automatically changed in response
    to updating an ASP file into the server.  For example, a link named  
    by ABC.asp - 7/1/2007 will change to ABC.asp - 7/15/2007, after I
    inserted an updated ABC.asp to the server folder today, i.e 7/15/2007.

    1. Can I create a similar ASP.NET 2.0 page with ABC.asp - 7/15/2007? How?  
    2. Is "ABC.asp - date" a good ASP.net 2.0 presentation?

    TIA,
    Jeffrey

  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-15-2007, 10:53 PM
    • All-Star
      89,572 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,319
    • Moderator
      TrustedFriends-MVPs

    You could make a user control like below.  Save the code to HyperLinkWithDate.ascx, add the file to your project, drag and drop the control onto a page and set the control's HyperLink.NavigateUrl property to a file.

     

    <%-- HyperLinkWithDate.ascx--%>
    
    <%@ Control Language="C#" ClassName="HyperLinkWithDate" %>
    
    <script runat="server">
    
        // ---- DateLabel_Load -------------------------------
        //
        // get the file date of the file in the hyperlink and
        // display it.
        
        protected void DateLabel_Load(object sender, EventArgs e)
        {
            DateTime FileDate;
            
            // assumes NavigateUrl is of form ~\MyFile.aspx
            FileDate = System.IO.File.GetLastWriteTime(Server.MapPath(HyperLink.NavigateUrl));     
    
            DateLabel.Text = FileDate.ToString(": d-M-yyyy");       
        }
        
    </script>
    
    <asp:HyperLink ID="HyperLink" runat="server" NavigateUrl="~/Default2.aspx" >Default2.aspx</asp:HyperLink>
    <asp:Label ID="DateLabel" runat="server" Text="Label" OnLoad="DateLabel_Load" ></asp:Label>
    
     
    Steve Wellens

    My blog
  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-16-2007, 1:59 PM
    • Member
      111 point Member
    • wonjartran
    • Member since 06-12-2007, 10:19 AM
    • Posts 659

    Thanks.

    I have coded the user control.  It worked very well, except that it showed 24-16-2007
    for 07/16/2007 12:24 PM of my aspx file.  I tried different date formates, e.g. mm/dd/yyyy,
    or dd/mm/yyyy, it still showed "24" month.  Not sure what's wrong.

    Jeffrey

  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-16-2007, 2:42 PM
    • Member
      111 point Member
    • wonjartran
    • Member since 06-12-2007, 10:19 AM
    • Posts 659

    Thanks.  Another question on creating the ascx files.

    1. If I have 10 links on an aspx web page, I have to create and drop 10 ascx files for 10 links?

    2. I have used a table to list my <a href="http://..."> on each TD.  Can I code the DateLabel_load
        directly behind the <a href="http...> without using the user control?

    3.  I have used Visual Baisc for my aspx web site.  Adding user controls with C# should be okay.
         Am I correct?

    TIA,
    Jeffrey

  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-16-2007, 5:38 PM
    Answer
    • All-Star
      89,572 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,319
    • Moderator
      TrustedFriends-MVPs

    Note the MM-mm is case sensitive.  You are getting minutes not Months.

    >> Can I code the DateLabel_load directly behind the <a href="http...> without using the user control? 

    Sure.

    >>I have used Visual Baisc for my aspx web site.  Adding user controls with C# should be okay.  Am I correct? 

    Try it and see :)

    By the way, I forgot to expose properties you can set in the properties window: 

    <%-- HyperLinkWithDate.ascx--%>
    
    <%@ Control Language="C#" ClassName="HyperLinkWithDate" %>
    
    <script runat="server">
    
        // ---- DateLabel_Load -------------------------------
        //
        // get the file date of the file in the hyperlink and
        // display it.
        
        protected void DateLabel_Load(object sender, EventArgs e)
        {
            DateTime FileDate;
            
            // assumes NavigateUrl is of form ~\MyFile.aspx
            FileDate = System.IO.File.GetLastWriteTime(Server.MapPath(HyperLink.NavigateUrl));     
    
            DateLabel.Text = FileDate.ToString(": d-M-yyyy");       
        }
    
        public String NavigateUrl
        {
            get { return HyperLink.NavigateUrl; }
            set { HyperLink.NavigateUrl = value;}
        }
        public String HyperLinkText
        {
            get { return HyperLink.Text; }
            set { HyperLink.Text=value;}
        }   
        
    </script>
    
    <asp:HyperLink ID="HyperLink" runat="server" NavigateUrl="~/Default2.aspx" >Default2.aspx</asp:HyperLink>
    <asp:Label ID="DateLabel" runat="server" Text="Label" OnLoad="DateLabel_Load" ></asp:Label>
    
     

    Now the usage is:

            <uc1:HyperLinkWithDate ID="HyperLinkWithDate1" runat="server"  HyperLinkText="Default1.aspx"  NavigateUrl="~\Default1.aspx" />
            <uc1:HyperLinkWithDate ID="HyperLinkWithDate2" runat="server"  HyperLinkText="Default2.aspx" NavigateUrl="~\Default2.aspx"/>
    
     

     

    Steve Wellens

    My blog
  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-17-2007, 2:18 PM
    • Member
      111 point Member
    • wonjartran
    • Member since 06-12-2007, 10:19 AM
    • Posts 659

    Yes, I have started coding using C# user control and it worked fine except for the mm problem. 
    After correcting the MM/mm problem, the user control worked perfectly.

    Then I tried to use in-line coding to code directly the DateLabel_Load into my aspx source file.
    Because my aspx file was coded by Visual Basic, I modified your DateLabel_Load C# to VB
    codes and tried several times.  My VB codes did not work for label_load.

    Some error msg are:
      'DateTime' is a type and cannot be used as an expression.
       Error 1 'FileDate' is not a member of 'Date'.

    Can you give me some sample VB codes for defining the MapPath for links to other aspx files and
    href url's?  

    TIA,
    Jeffrey

  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-18-2007, 8:59 AM
    • Member
      111 point Member
    • wonjartran
    • Member since 06-12-2007, 10:19 AM
    • Posts 659

    Thanks.

    Yes, I have written the VB code for a user control and have successfully showed the DateLabel Text.

    Then I wrote an in-line VB code into the aspx file with a href http link and received an error msg that
    the http file can not be found. Apparently the MapPath() works only for local file.  How do I code the
    file.GetLastWriteTime() for a http link?

    TIA,
    Jeffrey

  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-18-2007, 4:47 PM
    • Member
      111 point Member
    • wonjartran
    • Member since 06-12-2007, 10:19 AM
    • Posts 659

    Just tried the inline VB code:

     Protected Sub DateLabel4_Load(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim FileDate As DateTime
            FileDate = System.IO.File.GetLastWriteTime("\\sca4001\DARS DDS\Home.aspx")
            DateLabel4.Text = FileDate.ToString(": MM/dd/yyyy")
        End Sub

    sca4001 is my other server.  Many times, including the local server,
    the FileDate seemed to be working, but the label date was 12/31/1600.
    What does 12/31/1600 mean?  The coding was correct, but the ToString was wrong?
    Or the FileDate is still wrong, so the filedate is empty?

    TIA,
    Jeffrey

  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-18-2007, 6:07 PM
    • All-Star
      89,572 point All-Star
    • SGWellens
    • Member since 01-02-2007, 4:27 PM
    • Twin Cities, MN
    • Posts 7,319
    • Moderator
      TrustedFriends-MVPs

    wonjartran:
    What does 12/31/1600 mean? 

    Online Help:  If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

    Steve Wellens

    My blog
  • Re: Code an ASP.net 2.0 link with an automatic updated date

    07-18-2007, 9:18 PM
    • Member
      111 point Member
    • wonjartran
    • Member since 06-12-2007, 10:19 AM
    • Posts 659

    Thanks.

    I changed the path to C:\Inetpub\wwwroot\....; then the FileDate worked and
    the correct date was shown in the Label control.  Apparently, the File.GetLastWriteTime 
    worked strictly only for local c: drive files, not for http or network files..

    Thanks,
    Jeffrey

Page 1 of 1 (10 items)