"Hello World" in C# for ASP.Net

The most basic of a programs is how to display some text. Here are a few ways to do this in ASP.Net using C#.

The most sparse is to use in-line code. In this example, there is string declared in-line and then another in-line block to write the response back to the browser.

<%@ Page Language="C#" %>
<%
  string output = "Hello World.";
%>
<html>
<body>
  <% Response.Write(output); %>
</body>
</html>

A slightly more elegant approach is to use the Page_Load() function and to attach some text to a label. This looks like this:

<%@ Page Language="C#" %>
<script runat="server">
    protected void Page_Load(object sender, System.EventArgs e)
    {
        Label1.Text = "Hello World!";
    }
</script>
<html>
<body>
  <asp:Label ID="Label1" runat="server"></asp:Label>
</body>
</html>

No comments: