TOC

This article is currently in the process of being translated into Chinese (~3% done).

The basics:

Hello, world!

在幾乎每個程式語言指導內,你將找到最經典"Hello, world!"範例,我何必去打破這個傳統呢?讓我展示給您如何從ASP.NET對世界說哈囉。在Solution Explorer使用雙擊,開啟Default.aspx(假如它尚未開啟)。它已經包含一堆HTML標記語言,和一些您可能不認得的東西,如在頂端的頁面指引,或是form標籤的runat屬性。這所有都將在之後解釋,現在,我們想要看一些執行程式碼。

首先,我們將加一個Label控制項到頁面。一個Label控制項是有點簡單,因為它只是去保持住一些文字。增加下列像HTML程式碼在<form>標籤組之間。

<asp:Label runat="server" id="HelloWorldLabel"></asp:Label>

Secondly, add this script block somewhere on the page, preferably below the Page directive in the top:

<%
    HelloWorldLabel.Text = "Hello, world!";
%>

If you haven't worked with ASP.NET before, I'm sure there's a bunch of things that you're wondering about now, but as I said, this is all about seeing some results right now. To see the page in action, use Debug -> Start Without Debugging, or simply press F6. Visual Studio will now compile your project, and launch the page you're working on in your default browser. The page will simply have a piece of text which says "Hello, world!" - congratulations, you have just created your first ASP.NET website! Here is the complete listing:

<%
    HelloWorldLabel.Text = "Hello, world!";
%>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:Label runat="server" id="HelloWorldLabel"></asp:Label>
    </div>
    </form>
</body>
</html>
This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!