簡體   English   中英

asp.net C#變量

[英]asp.net C# variables

這可能是非常簡單的,如果我看起來完全愚蠢,對不起,...

我正在為類編寫我的第一個asp.net網頁,並且習慣了PHP

在PHP中,當您在一個代碼塊中創建變量時,可以在所有其他代碼塊中使用它(只要它已被初始化)

這是我的意思的示例:

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

(稍后在代碼中:)

<?php echo $myVariable; ?>

並且在PHP中可以正常工作

現在,據我所知,asp中的等價物是:

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

但是,當我嘗試在另一個代碼塊中使用它時,變量myVariable無法訪問

難道我做錯了什么?

這里的問題是變量的范圍。 我建議您閱讀http://www.informit.com/articles/article.aspx?p=25467&seqNum=5的這篇文章

更新資料

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM