繁体   English   中英

编译器错误消息:CS0103:当前上下文中不存在名称“Test1”

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

我对ASP.NET中的编程完全不熟悉,并且不理解为什么错误“编译器错误消息:CS0103:名称'Test1'在当前上下文中不存在”出现在这个基本代码段上。

下面是我要编译的代码 -

<%@ 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>

有关我收到此错误消息的原因的任何帮助将不胜感激。

您需要在asp控件上包含runat="server"属性,否则您的代码隐藏无法识别它。

注意,您也可以将此属性添加到纯HTML元素中。

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

myLabel.InnerText = "stuff";

除了包含runat标记之外,您没有正确关闭标记

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

要么

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM