【问题标题】:Compiler Error Message: CS0103: The name 'Test1' does not exist in the current context编译器错误消息:CS0103:当前上下文中不存在名称“Test1”
【发布时间】:2013-03-26 06:07:55
【问题描述】:

我是 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.net


    【解决方案1】:

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

    注意,您也可以将此属性添加到普通的 html 元素中。

    <label id="myLabel" runat="server">text</label>
    
    myLabel.InnerText = "stuff";
    

    【讨论】:

    • 非常感谢!非常感谢!
    【解决方案2】:

    除了包含runat标签,你没有正确关闭标签

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2019-01-05
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多