ASP.NET 網頁中的嵌入式代碼塊 與

<% %>是嵌入代碼塊 ,而<%= %>是嵌入表達式

我們都知道,Response.Write 方法能將信息寫入HTTP輸出內容數據流,而嵌入表達式可以作爲調用Response.Write方法的快捷方式。

比如:<%="好嗎"%> 和<%Response.Write("好嗎")%>的結果是完全相同的。

 

實例:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>示範怎樣使用顯示程序代碼語法</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table width="300" border="0" cellspacing="1" cellpadding="1">
            <tr>
                <td style="text-align: center">
                    <%  int i;
                        for (i = 0; (i <= 7); i++) {%>
                    <font color="#ff0066" size="<%=i%>"><b>歡迎光臨</b></font>
                    <br />
                    <% }%>
                    <% for (i = 7; (i >= 0); i--) {%>
                    <font color="#ff0066" size="<%=i%>"><b>歡迎光臨</b></font>
                    <br />
                    <% }%>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

 

注意:如果在語法標記<%...%>中定義的程序塊中使用了字符串'%>'將會發生編譯錯誤。這個字符串只能用來結束程序代碼塊,

如下,會出錯

<%

    Response.Write("%>");

%>

 

要避免發生這種錯誤,必須如下定義一個含有這些字符的字符串變量。

<%

    String s;

    s="%"+">";

    Response.Write(s);

%>

 

此外還要注意代碼的服務器端批註語法<%--   --%>,

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章