Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
asp.net C#变量_C#_Asp.net - Fatal编程技术网

asp.net C#变量

asp.net C#变量,c#,asp.net,C#,Asp.net,这可能非常简单,如果我看起来很愚蠢,我很抱歉,但是 我正在为一个类编写我的第一个asp.net网页,我已经习惯了PHP 在PHP中,当您在一个代码块中创建变量时,您可以在所有其他代码块中使用它(只要它已经初始化) 以下是我的意思的一个例子: <?php $myVariable = "Hello World!" ?> (后面的代码:) 在PHP中,这将很好地工作 现在,据我所知,asp中的等价物是: <% String myVariable = "Hello World

这可能非常简单,如果我看起来很愚蠢,我很抱歉,但是

我正在为一个类编写我的第一个asp.net网页,我已经习惯了PHP

在PHP中,当您在一个代码块中创建变量时,您可以在所有其他代码块中使用它(只要它已经初始化)

以下是我的意思的一个例子:

<?php $myVariable = "Hello World!" ?>

(后面的代码:)


在PHP中,这将很好地工作

现在,据我所知,asp中的等价物是:

<% String myVariable = "Hello World!"; %>

但是当我试图在另一个代码块中使用它时,变量myVariable是不可访问的


我做错什么了吗?

这里的问题是变量的范围。我建议你在

更新

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    string myVariable = "This is a variable";
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%=myVariable %>
    </div>
    </form>
</body>
</html>

string myVariable=“这是一个变量”;

这里的问题是变量的范围。我建议你在

更新

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    string myVariable = "This is a variable";
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <%=myVariable %>
    </div>
    </form>
</body>
</html>

string myVariable=“这是一个变量”;

这在ASP.NET中是正常的、预期的行为。这在ASP.NET中是正常的、预期的行为。所以,我必须使用?@Axschech,这是所有非PHP web语言的标准。但我建议使用ASP.NET的代码隐藏方法,而不是使用内联方法,以提高代码的可维护性。谢谢大家,抱歉我是一个derp!所以,我必须使用?@Axschech,这是所有非PHP web语言的标准,但我建议使用asp.net的代码隐藏方法,而不是使用内联方法,以提高代码的可维护性。谢谢你们,抱歉,我是一个derp!