Time Of Day C# .ASP Project

Hello, this is my first quick project in my exploration of how the C# .NET programming language can be used to create / use ASP pages. I will have a separate section for ASP code from the regular C# Downloads & Files for organizational reasons.

My first project delving into this is a simple time of day page. You enter the name in the text field and based on the time of day it will display “Good Morning”, “Good Afternoon”, or Good Evening” followed by the name entered. I’ve also added a small condition to make sure something is entered. Otherwise there’s a notification.

Live Page Example.

Main Page & Error Display

TimeOfDayMainPage TimeOfDayMainPageError


Default.aspx Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Time Of The Day</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    &nbsp;<asp:Label
            ID="dateTimeOfDayLabel" runat="server" Text="Date / Time Display"></asp:Label>
        <br />
        <br />
        <asp:Label ID="timeOfDayNameLabel" runat="server" Text="Enter Your Name:"></asp:Label>
&nbsp;<asp:TextBox ID="timeOfDayNameTextBox" runat="server"></asp:TextBox>
&nbsp;<asp:Button ID="timeOfDayGoButton" runat="server" Text="Go!" />
        <br />
        <asp:Label ID="timeOfDayGreetingLabel" runat="server" Text="...."
            Visible="False"></asp:Label>

    </div>
    </form>
</body>
</html>

Default.aspx Code Behind File

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    //Private class variables.
    private DateTime now = DateTime.Now;        //Getting the date and time.

    protected void Page_Load(object sender, EventArgs e)
    {
        //Displaying the date and time.
        dateTimeOfDayLabel.Text = now.ToString();

        //Lambda expression for the Go button on the webpage.
        timeOfDayGoButton.Click += (from, ea) => GetNameAndTime();
    }

    //This method gets the name entered and the current data/time.
    private void GetNameAndTime()
    {
        //Making the label visible.
        timeOfDayGreetingLabel.Visible = true;

        //Getting the hour from the date and time.
        int hour = now.Hour;
    
        //Checking to make sure something was typed in the textbox.
        if (string.IsNullOrWhiteSpace(timeOfDayNameTextBox.Text))
            timeOfDayGreetingLabel.Text = "Please put in your name";
        else
        {
            //Good morning display.
            if (hour <= 11)
                timeOfDayGreetingLabel.Text = "Good Morning " + timeOfDayNameTextBox.Text;
            //Good afternoon display.
            else if (hour <= 18)
                timeOfDayGreetingLabel.Text = "Good Afternoon " + timeOfDayNameTextBox.Text;
            //Good evening display.
            else
                timeOfDayGreetingLabel.Text = "Good Evening " + timeOfDayNameTextBox.Text;
        }
    }
}
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s