共用方式為


ASP.NET 中的程式碼區塊

ASP.NET 單一檔案網頁模型類似 Active Server Pages (ASP) 網頁結構,相似處在於包含程式碼的指令碼區塊會出現在 HTML 之前,以及在 HTML 中能夠使用特殊標記 (Markup) 型態的標記 (Tag)。這個主題描述將 ASP 程式碼更新為 ASP.NET 會牽涉到的問題。

ASP.NET 程式碼後置網頁模型會將指令碼區塊程式碼與 HTML 和 ASP.NET 標記 (Markup) 型態的標記 (Tag) 加以區隔開。如需詳細資訊,請參閱 ASP.NET Web 網頁程式碼模型

宣告變數和程序

所有 ASP.NET 程序和全域變數都應該在置於 <html> 標記前的 <script runat="server"> 區塊內宣告,而不是在 ASP <%...%> 樣式分隔符號之間宣告。您仍然可以在 <%...%> 轉譯區塊中宣告變數,但是這些變數只能夠讓網頁上的其他轉譯區塊存取,而不能讓其他函式或程序全域存取。網頁可以包含一個以上的 <script> 區塊,但是網頁上所有區塊的程式語言必須相同。

下列程式碼範例示範在指令碼和轉譯區塊中,宣告變數與程序的 ASP.NET Web 網頁。如果您在 URL 中將查詢字串傳遞至 ASP.NET Web 網頁,網頁上就會顯示查詢字串。

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
    ' The following variables are visible to all procedures 
    ' within the <script> block.
    Dim str As String
    Dim i, i2 As Integer

    Function DoubleIt(ByVal inpt As Integer) As Integer
        ' The following variable is visible only within 
        ' the DoubleIt procedure.
        Dim factor As Integer = 2

        DoubleIt = inpt * factor
    End Function
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% 
        ' The following local variable is visible only within this
        ' and other render blocks on the page.
        Dim myVar

        myVar = Request.QueryString
        Response.Write("The querystring is " _
           & Server.HtmlEncode(myVar.ToString()))
    %>
    </div>
    </form>
</body>
</html>
<%@ 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">
    // The following variables are visible to all procedures 
    // within the <script> block.
    String str;
    int i;
    int i2;

    int DoubleIt(int inpt)
    {
        // The following variable is visible only within 
        // the DoubleIt procedure.
        int factor = 2;

        return inpt * factor;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <% 
      // The following local variable is visible only within this
      // and other render blocks on the page.
      object myVar;

      myVar = Request.QueryString;
      Response.Write("The querystring is " + 
         Server.HtmlEncode(myVar.ToString()));
    %>
    </div>
    </form>
</body>
</html>

轉譯文字

ASP.NET 並不支援轉譯函式。使用舊版 ASP 時,您可在程序主體內插入常值 HTML,如下列程式碼範例所示:

   <% Sub SomeProcedure() %>
   <H3> Render this line of bold text. </H3>
   <% End Sub %>

這個程式碼會在 ASP.NET Web 網頁上產生錯誤。對於 ASP.NET,您將必須撰寫如下所示的程式碼:

   <script runat=server>
      Sub SomeProcedure()
         Response.Write("<H3> Render this line in bold text.</H3>")
      End Sub
   </script>

所有包含在 <script>…</script> 標記中的程式碼,除了全域變數宣告外,都必須封裝在程序內。

網頁處理、載入和卸載

使用 ASP 時,程式碼會放在 <%...%> 標記內,並且會從第一個 <%> 標記之後的第一個陳述式開始處理網頁。使用 ASP.NET 時,載入網頁後要立即處理的任何程式碼,都必須包含在 Page_Load 內建事件內。您仍然可以在 <%...%> 區塊中撰寫程式碼,但是會在載入網頁後的轉譯階段才會執行該程式碼 (和在 ASP 中一樣,以由上而下的方式執行)。如果您需要執行初始化程式碼,它應在 Page_Load 事件中出現,這個事件會在 ASP.NET 引擎載入網頁後立即引發,如下列程式碼範例所示。

<%@ Page Language="VB" %>

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

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' Place all startup code here, including initialization of 
        ' variables,  preloading of controls, and loading of 
        ' data from a database.
        If Page.IsPostBack Then
            Response.Write("<br />Page has been posted back.")
        End If

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Hello.<br />
    <asp:Button ID="Button1" runat="server" 
    Text="Click to Post Back" />
    </div>
    </form>
</body>
</html>
<%@ 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">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Response.Write("<br />Page has been posted back.");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Hello.<br />
    <asp:Button ID="Button1" runat="server" 
    Text="Click to Post Back" />
    </div>
    </form>
</body>
</html>

相關的 Page_Unload 內建事件一定是網頁執行生命週期期間最後引發的事件,並且可以用來執行任何網頁清除程式碼。

如需詳細資訊,請參閱 ASP.NET 網頁生命週期概觀ASP.NET Web 伺服器控制項事件模型

請參閱

概念

ASP.NET 網頁類別概觀
ASP.NET Web 網頁程式碼模型
ASP.NET Web 網頁模型的新功能
ASP.NET Web 網頁語法概觀
ASP.NET 網頁生命週期概觀
ASP.NET Web 伺服器控制項事件模型

其他資源

移轉至 ASP.NET
以程式設計包含用戶端指令碼的 ASP.NET Web 網頁