You are currently browsing the category archive for the 'Time Based Greeting in ASP.NET' category.
Objective: This article describes how to display a time based greeting on your asp.net page.
Background: I use this script sometimes to greet the logged in user. It is a nice touch and sometimes you just got to have it. You can also modify it to combine with other procedures and element on your page.
Procedure: Follow instructions below to accomplish this task. This article uses Visual Web Developer Express 2008.
- Create a new page in your site and replace all code with the following or just study the code between the script blocks to figure out how you can add it to an existing page.
<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" _ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> '...........................Time Based Greeting Start......................... 'execute the following subprocedure on page load 'set the time label to the current time, add a space and a dash in front of it 'set the date label to current date 'if the hour now is less 12 noon then 'set the greeting label to good morning 'else if the current hour is less than 5 PM then 'set the greeting label to good afternoon 'else 'set the greeting label to good evening 'end if statement 'end subprocedure Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) lblTime.Text = " -" & Now().ToLongTimeString lblDate.Text = Now().ToLongDateString If Now().Hour < 12 Then lblGreeting.Text = "Good morning!" ElseIf Now().Hour < 17 Then lblGreeting.Text = "Good afternoon!" Else lblGreeting.Text = "Good evening!" End If End Sub '...........................Time Based Greeting End......................... </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="lblGreeting" runat="server" /> <asp:Label ID="lblDate" runat="server" /> <asp:Label ID="lblTime" runat="server" /> </div> </form> </body> </html>
- That’s it! Enjoy.
