[英]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. 我对ASP.NET中的编程完全不熟悉,并且不理解为什么错误“编译器错误消息:CS0103:名称'Test1'在当前上下文中不存在”出现在这个基本代码段上。
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. 有关我收到此错误消息的原因的任何帮助将不胜感激。
You need to include the runat="server"
attribute on your asp control, otherwise your code-behind does not recognize it. 您需要在asp控件上包含
runat="server"
属性,否则您的代码隐藏无法识别它。
Note, you can add this attribute to plain html elements as well. 注意,您也可以将此属性添加到纯HTML元素中。
<label id="myLabel" runat="server">text</label>
myLabel.InnerText = "stuff";
In addition to including the runat tag, you didn't close the tag correctly 除了包含runat标记之外,您没有正确关闭标记
<asp:Label runat="server" ID="Test1"></asp:Label>
or 要么
<asp:Label runat="server" ID="Test1"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.