Isiah Johnson
Isiah Johnson

Reputation: 25

ASP.NET Labels aren't displaying the expected value

I need help on this assignment question:

When user visits the website, it's supposed to display the greeting message “Good Morning” if the time the user visiting is in the morning. If it is afternoon, it should display “Good afternoon”; if it is at night, it should display “Good evening”.

I am a little confused as to why the labels in this ASP.NET website are not displaying the greetings at all.

My code snippet:

<%@ Page Language="VB" AutoEventWireup="false" 

CodeFile="Default.aspx.vb" Inherits="Labs_7_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body onload="">
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblGreeting" runat="server"/>
    </div>
        </form>
</body>
</html>

This is the code behind:

Partial Class Labs_7_Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    If DateTime.Now.Hour >= 6 And DateTime.Now.Hour < 12 Then
        lblGreeting.Text = "Good morning"
    ElseIf DateTime.Now.Hour >= 12 And DateTime.Now.Hour < 18 Then
        lblGreeting.Text = "Good afternoon"
    Else
        lblGreeting.Text = "Good evening"
    End If
End Sub

I have also tried other variations of this code but the labels are never shown in the browser.

Upvotes: 1

Views: 58

Answers (1)

Martin Parenteau
Martin Parenteau

Reputation: 73721

Some part of the ASPX file may be missing. The minimal markup should look like this:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblGreeting" runat="server"/>
    </div>
    </form>
</body>
</html>

WebForm1 and WebApplication1 may have different names in your application.

UPDATE

The real answer was given by Tim Medora. I missed the fact that Page_Load in your code-behind did not have Handles Me.Load:

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

If the Handles clause is there, AutoEventWireup can be false. Otherwise, it must be true.

Upvotes: 2

Related Questions