user2205892
user2205892

Reputation:

Compiler Error Message: CS0103: The name 'Test1' does not exist in the current context

I am completely new to programming in ASP.NET and do not understand why the error "Compiler Error Message: CS0103: The name 'Test1' does not exist in the current context" occurs on this basic piece of code.

Below is the code that I am trying to compile -

<%@ Page Language="C#" AutoEventWireup="true" %>

<!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>Untitled Page</title>

</head>

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

<asp:Label ID="Test1"/></asp>

<script runat="server">
 protected void Page_Load(object sender, EventArgs e)
{
Test1.Text = "Hello World; the time is now" + DateTime.Now.ToString();
}
</script>

</div>
</form>

</body>
</html>

Any assistance on why I'm receiving this error message would be greatly appreciated.

Upvotes: 3

Views: 7889

Answers (2)

jugg1es
jugg1es

Reputation: 1600

In addition to including the runat tag, you didn't close the tag correctly

<asp:Label runat="server" ID="Test1"></asp:Label>

or

<asp:Label runat="server" ID="Test1"/>

Upvotes: 2

Brad M
Brad M

Reputation: 7898

You need to include the runat="server" attribute on your asp control, otherwise your code-behind does not recognize it.

Note, you can add this attribute to plain html elements as well.

<label id="myLabel" runat="server">text</label>

myLabel.InnerText = "stuff";

Upvotes: 2

Related Questions