[代碼]為ASP.NET網頁編寫嵌入式代碼

來源:互聯網
上載者:User

此文章中的兩個範例程式碼主要用來示範如何使用ASP.NET所提供的嵌入式編碼方式。

樣本1
此範例程式碼在網頁中添加了一個for語句的代碼塊,迴圈輸出1~6這6個數字。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>ASP.NET網頁中的嵌入式代碼塊!</title></head><body>    <form id="form1" runat="server">    <div>        <% for (int Index = 0; Index < 7; Index++)           {               Response.Write(Index.ToString());               if (Index != 6)               {                   Response.Write("<br />");               }           } %>    </div>    </form></body></html>

這個範例程式碼用的嵌入式代碼文法是<% expression %>

樣本2
此範例程式碼在嵌入式代碼塊中調用了當前頁中的一個名為GetTime()的函數,此函數返回伺服器的目前時間。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!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>ASP.NET網頁中的嵌入式代碼塊!</title></head><body>    <form id="form1" runat="server">    <div>        <script runat="server">            protected string GetTime()            {                return DateTime.Now.ToString("t");            }            </script>        <span>            <%= GetTime() %>        </span>    </div>    </form></body></html>

這個嵌入式代碼用的文法是<%= expression %>。
注意不要在GetTime()的後面添加分號,否則是會出現錯誤的。注意和前一個文法的比較!

最後說明一下:嵌入式代碼塊的程式設計語言是當前頁面的預設語言,也就是由@Page指令的Language參數所指定的語言。

光腳丫思考 2010-11-9

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.