Team LiB
Previous Section Next Section

Creating an ASP.NET "Hello World" Application

In this section, you will create your first ASP.NET application. This application is a simple but useful Hello World application. This application, upon execution, displays the message Welcome from ASP.NET Web Forms!, as shown in Figure 21.9. This application is simple in that you only have to drag and drop a Label component on the form and change the Text property; no coding is required. To create this application, first select New Project from the File, New, Project menu, as shown in Figure 21.3.

Figure 21.9. Modify the Text property to set the message.


Figure 21.3. Select New Project from the File, New, Project menu.


Next, select the ASP.NET Web Application from the Visual C# Projects menu, as shown in Figure 21.4, and enter http://localhost/HelloWorld in the Location section of the dialog.

Figure 21.4. Select ASP.NET Web Application to create the Hello World application.


This will create a new C# ASP.NET application in a directory off of the Web Root folder. Visual Studio .NET will also create a virtual directory and point it to the specified location, as shown in Figure 21.5.

Figure 21.5. A look at Visual Studio .NET creating the Hello World application.


After the Hello World application has been created, you can see the application and all of its files in the Solution Explorer, as shown in Figure 21.6.

Figure 21.6. The Solution Explorer with the new Hello World application displayed.


In the Solution Explorer, you can see that Visual Studio .NET not only created the project, but added four files as well. Three of these four files will always exist in every web application. These files are discussed in Table 21.1.

Table 21.1. Files Added as Part of the Default Web Application Template

File

Description

Web.config

This file contains all the configuration settings for the web application.

Global.asax

This file contains a class called Global that inherits from System.Web.HttpApplication. You can modify this file to wire up global event handlers, such as application start/end and session start/end, and any other event available from the System.Web.HttpApplication class.

AssemblyInfo.cs

This file contains all information about the assembly, such as general assembly information, version information, and information about signing the assembly.


The last file is the Web Form that was created for the application. By default, this form is given the name WebForm1. Now that you have an understanding of what Visual Studio .NET does when it creates a web application and what files are created along with the application, you can modify the form to display the welcome message. First, select the Web Forms tab from the toolbox and drag the Label component to the WebForm1 Design view, as shown in Figure 21.7. After you've dropped this component on the form, you will see a component that displays Label as its Text property, as shown in Figure 21.8.

Figure 21.7. Drag and drop the label from the toolbox onto the Web Form.


Figure 21.8. Design view after the Label control is dropped on the form.


Now change the message displayed by the Label component from Label to Welcome from ASP.NET Web Forms!. You do this by selecting the Label component and modifying the Text property in the Properties window, as shown in Figure 21.9.

Now that the application is finished and displays the correct message in the Design view, as shown in Figure 21.10, it is time to run the application and see whether the application does what you expect.

Figure 21.10. The Design view of the Hello World application.


To run the application, select the Debug, Start menu item or simply press the F5 key (assuming that you are using the Visual Studio Developer profile). When the program is running, you should be able to see the application in the browser and it should display the message Welcome from ASP.NET Web Forms!, as shown in Figure 21.11.

Figure 21.11. The Hello World application in action.


Listings 21.1 and 21.2 show the complete source code that it took to create the Hello World application. Listing 21.1 is the HTML code that displays the Web Form. The following line is the only line that was necessary to add in order to display the desired message :

<asp:Label id="Label1" style="Z-INDEX: 101; LEFT: 48px;
 POSITION: absolute; TOP: 40px" runat="server">Welcome from ASP.NET Web Forms!</asp:Label>

All other lines of code were generated by the ASP.NET Application Wizard.

Listing 21.1. WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
  AutoEventWireup="false" Inherits="HelloWorld.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
  <HEAD>
    <title>WebForm1</title>
    <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" Content="C#">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/
                intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
      <asp:Label id="Label1" 
         style="Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 40px"
         runat="server">Welcome from ASP.NET Web Forms!</asp:Label>
    </form>
  </body>
</HTML>

Listing 21.2. WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace HelloWorld
{
  /// <summary>
  /// Summary description for WebForm1.
  /// </summary>
  public class WebForm1 : System.Web.UI.Page
  {
    protected System.Web.UI.WebControls.Label Label1;

    private void Page_Load(object sender, System.EventArgs e)
    {
        // Put user code to initialize the page here
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
        //
        // CODEGEN: This call is required by the ASP.NET Web Form Designer.
        //
        InitializeComponent();
        base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
      this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
  }
}

    Team LiB
    Previous Section Next Section