Lesson 1: Creating an ASP.NET Web Application Project

Lesson 1: Creating an ASP.NET Web Application Project

Web forms are the application objects that define the user interface of your Web application. The text and controls you place on a Web form determine what a user sees when he or she runs your application. In this way, Web forms are similar to Windows forms in a Windows application.

Unlike Windows applications, however, Web applications run on a server and are distributed to clients through the Internet. Therefore, how you create and organize a Web application project has significant differences from working with Windows applications in Visual Studio .NET. These differences are explained in the following sections.

After this lesson, you will be able to

  • Start a Web Forms project in Visual Studio .NET

  • Set the physical location where your projects will be stored

  • Add server controls to a Web form and write code to respond to events on those controls

  • Run the Web application in the programming environment

  • Identify the purpose of the files in a Web Forms project

Estimated lesson time: 30 minutes

Creating a New Web Application

The first step in creating an ASP.NET Web application is to start a new project in Visual Studio .NET. Visual Studio .NET provides templates for each type of application you can create. The template for a Web application is named ASP.NET Web Application. When you create a new project using this template, Visual Studio .NET creates a project file; a new, blank Web form; and other supporting files used by your application.

To create a Web application project from Visual Studio .NET, follow these steps:

  1. On the Visual Studio .NET Start Page, click New Project. Visual Studio .NET displays the New Project dialog box, as shown in Figure 2-1. Visual Studio .NET puts Web applications in the localhost virtual directory.

    figure 2-1 the new project dialog box

    Figure 2-1. The New Project dialog box

  2. In the New Project dialog box, select the ASP.NET Web Application template, type the name of the new project in the Location text box, and click OK.

When you create a Web application project in Visual Studio .NET, the programming environment creates a new folder and generates a set of files for the project. Visual Studio .NET gives the folder the same name as the project and puts the folder in the root folder of the default Web site that IIS hosts on your computer. This location is shown in the Location box of the New Project dialog box as http://localhost/projectname.

Organizing Your Projects with IIS

It s important to realize that Web applications can exist only in a location that has been published by IIS as a virtual folder. A virtual folder is a shared resource identified by an alias that represents a physical location on a server. If you try to select a physical folder from the New Project dialog box, such as C:\MyFiles, Visual Studio .NET disables the OK button. It s not OK you can t create a Web application there!

The virtual folder named //localhost is the Web root folder on your computer. IIS determines the physical location of your Web root folder. By default, IIS installs the folder on your boot drive at \Inetpub\wwwroot. Instead of using the //localhost default and letting it get cluttered with sample projects, production code, and other stuff, organize your projects by creating separate folders for samples, tests, and production code. Then share those folders with the Web before you create new projects. Remember that Visual Studio .NET creates a new folder for each new project, so create only the folders you need to organize the types of projects you create.

Creating Virtual Folders to Organize Web Applications

Use IIS to create new virtual folders and to manage Web sites hosted on your computer. Creating a virtual folder for use with Visual Studio .NET requires two major tasks:

  • Creating the virtual folder.

    Virtual folders specify where your Web application projects are physically stored, so you use them to help organize your projects during development.

  • Adding the FrontPage Server Extensions to the virtual folder to create a subweb.

    A subweb is simply a virtual folder that contains a Web site. Adding the FrontPage Server Extensions to a virtual folder enables Visual Studio .NET to create and maintain Web applications in that folder.

Creating a Virtual Folder

To create a new virtual folder in IIS, follow these steps:

  1. Open the IIS management console by clicking Start, pointing to All Programs, Administrative Tools, and clicking Internet Information Services.

  2. In the console tree, expand the local computer and Web sites. Right-click Default Web Site, point to New, and click Virtual Directory on the shortcut menu, as shown in Figure 2-2. The Virtual Directory Creation Wizard opens.

    figure 2-2 creating a new virtual folder

    Figure 2-2. Creating a new virtual folder

  3. IIS starts the Virtual Directory Creation Wizard to walk you through creating a new virtual folder. Click Next on the wizard title page to display the Virtual Directory Alias page, as shown in Figure 2-3.

    figure 2-3 the virtual directory alias page

    Figure 2-3. The Virtual Directory Alias page

  4. Type an alias for the folder. The alias is the name you will use to identify the resource in this folder. In Visual Studio .NET, this is the name you will use to specify the location of your project. Click Next. The wizard displays the Web Site Content Directory page, as shown in Figure 2-4.

    figure 2-4 the web site content directory page

    Figure 2-4. The Web Site Content Directory page

  5. In the Directory box, type the path specification of the physical folder to associate with the virtual folder. This is the base folder where your project folders will be stored. Click Next. The wizard displays the Access Permissions page, as shown in Figure 2-5.

    figure 2-5 the access permissions page

    Figure 2-5. The Access Permissions page

  6. Keep the default permissions shown in Figure 2-5. Click Next and then click Finish to create the virtual folder and complete the wizard.

Creating a Subweb

To add the FrontPage Server Extensions to a virtual folder, follow these steps:

  1. Right-click the Default Web Site icon in the IIS management console, point to New, and click Server Extensions Web on the shortcut menu.

  2. IIS starts the New Subweb Wizard to walk you through adding the FrontPage Server Extensions to your virtual folder. Click Next on the wizard title page to display the Subweb Name page, as shown in Figure 2-6.

    figure 2-6 the subweb name page

    Figure 2-6. The Subweb Name page

  3. Type the name of the virtual folder in the Directory Name box. This name corresponds to the alias you entered in step 4 of the previous procedure. Type a description of the folder in the Title box or leave it blank. Click Next. The wizard displays the Access Control page, as shown in Figure 2-7.

    figure 2-7 the access control page

    Figure 2-7. The Access Control page

  4. Accept the default access control settings by clicking Next, and then click Finish to create the subweb.

Creating a New Project in the Virtual Folder

After you create a new virtual folder and add the server extensions to it, you can use it within Visual Studio .NET to create new projects. To create a Web application project in your new virtual folder, choose New Project from the Visual Studio .NET File menu, and specify the name of the virtual folder in the New Project dialog box, as shown in Figure 2-8.

figure 2-8 create a project in the new virtual folder

Figure 2-8. Create a Project in the new virtual folder

The location you specify in the New Project dialog box takes the form http://servername/virtualfolder. The server name localhost indicates that the server is running on your development machine. The name virtualfolder is the alias you created for the virtual folder in the preceding tasks.

Adding Controls and Writing Code

When Visual Studio .NET creates a Web application, it displays a new Web form in the center window, as shown in Figure 2-9. Drag controls from the Toolbox to the Web form as you would with a Windows form.

figure 2-9 a new web form

Figure 2-9. A new Web form

To add code to respond to events on the Web form, double-click the control. Visual Studio .NET displays the Code window. Add your code to the event procedure provided for the control. For example, the following code displays Hello, Web! in Internet Explorer when you click the Button1 command button:

Visual Basic .NET

Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Response.Write("Hello, Web!<br>") End Sub

Visual C#

private void Button1_Click(object sender, System.EventArgs e) { Response.Write("Hello, Web!<br>"); }

Visual Studio automatically creates the event procedures for an object s default event when you double-click the control in the Design window. The Button1_Click event procedure shown above responds, or handles, the Click event for the Button control. In Microsoft Visual Basic .NET, the Handles clause at the end of the procedure declaration makes the connection between the Click event and this event procedure.

In Microsoft Visual C#, this connection is hidden in the code generated by the Web Forms Designer. To view this code, click the plus sign next to the Web Form Designer Generated Code region in the Code window. The following code shows the generated code connecting the Form Load event and Button Click events to event procedures.

Visual C#

#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.Button1.Click += new System.EventHandler(this.Button1_Click); this.Load += new System.EventHandler(this.Page_Load); } #endregion

Creating this connection between an object s event and the event procedure that responds to the event is called wiring the event. Visual Basic .NET automatically creates this connection through the Handles clause, but Visual C# requires you to wire the event manually by adding code to the InitializeComponent procedure for any of the nondefault events you want to use.

To run the application, press F5. Visual Studio .NET builds the application, starts the browser, and then displays the page in the browser. When you click Button1, the browser displays Hello, Web! as shown in Figure 2-10. Closing the browser ends the Web application.

figure 2-10 the web form

Figure 2-10. The Web form

As you can see from this example, Web Forms applications are similar to Windows Forms applications. However, Web Forms applications have the following significant differences:

  • Tools.

    Web forms can t use the standard Windows controls. Instead, they use server controls, HTML controls, user controls, or custom controls created specially for Web forms.

  • User interface.

    The appearance of a Web form is determined by the browser that displays it. Your Web application can appear in Internet Explorer, Netscape Communicator, or any other HTML-compliant browser. Different browsers (and different versions within browsers) support different HTML features, which can change the appearance and behavior of your Web forms. Don t get too alarmed, though. Server controls handle most of these differences gracefully, as you ll see in Chapter 4, Creating a User Interface.

  • Lifetime.

    Windows forms are instantiated, exist for as long as needed, and are destroyed. Web forms appear to behave this way, but in fact they are instantiated, sent to the browser, and then usually thrown away. This means that all of the variables and objects declared in a Web form are usually not available after the Web form is displayed! (It is possible to keep these variables and objects around after a Web form is displayed, but that is not the standard programming practice. More on this topic later.) To get anything interesting done, you need to save information in special state objects provided by ASP.NET. There s more on state objects in Chapter 3, Working with Web Objects.

  • Execution.

    The executable portions of a Web application live on the Web server. In this way, Web applications are sort of the ultimate client/server application: the browser is the only software installed on the client side, and all of the user interface and business logic runs on the server. All communication between the client and the server occurs through HTML. This means that even sophisticated Web applications pose little security risk to clients and therefore pass through firewalls undisturbed. The physical structure of a Web application and where processing occurs are explained in more depth in Lesson 3.

The Files in a Web Forms Project

The Web form is only one of 11 files Visual Studio .NET generates when it creates a new Web forms project. Table 2-1 describes the purpose of each of these project files. Visual Studio .NET creates these different files for each new Web application project. Only the files shown in bold are displayed in the Visual Studio .NET Solution Explorer.

Table 2-1. Web Forms Project Files

File name

Contains

AssemblyInfo.vb

AssemblyInfo.cs

All of the attributes that are written into the compiled assembly, including version, company name, GUID, and so on.

Global.asax

The global events that occur in your Web application, such as when the application starts or ends. You can have only one Global.asax file per project, and it exists in the root folder of the project.

Global.asax.vb

Global.asax.cs

The code used in Global.asax. This file is not shown in Solution Explorer.

Styles.css

The style definitions to use for the HTML generated by your project. This file appears only in Visual Basic .NET projects. For Visual C# projects, you can add it manually.

Web.config

The settings your Web server uses when processing this project. These settings specify how errors are reported, what type of user authentication to use, and so on.

Projectname.vsdisco

Descriptions of the Web Services that this project provides. This file is used for dynamic discovery of Web services (.asmx files) included in a Web application. This file is not shown in Solution Explorer.

WebForm1.aspx

The visual description of a Web form.

WebForm1.aspx.vb

WebForm1.aspx.cs

The code that responds to events on the Web form. By default, this file is not shown in Solution Explorer.

WebForm1.aspx.resx

The Extensible Markup Language (XML) resources used by the Web form. This file is not shown in Solution Explorer.

Projectname.vbproj

Projectname.csproj

The project file listing the files and settings used at design time. This file is not shown in Solution Explorer.

Projectname.vbproj.webinfo

Projectname.csproj.webinfo

This file tracks the root virtual folder for the Web application. This file is not shown in Solution Explorer.

In addition to the files listed in Table 2-1, Web application projects can contain any number of other file types. The primary types of files that you ll add to a Web application project are shown in Table 2-2. Web application projects will often include these different types of source files.

Table 2-2. File Types for Web Forms Projects

File extension

Project item

Description

.aspx

Web form

Each Web form constitutes an ASP.NET Web page in your application. Applications can have one or many Web forms. Web forms have code files associated with them with the file extension .aspx.vb. Visual C# forms have associated .aspx.cs files.

.htm

HTML page

Web pages that don t have server code can appear as HTML pages in your project.

.vb or .cs

Class or module

Code that defines objects in your application is stored in classes.

.ascx

Web user control

User controls that are built from other Web forms and server controls in Visual Studio .NET.

.asmx

Web service

Web services that expose classes for remote execution over a network, such as the Internet.

.xml

XML file

Data files that store information used by your application.

.xsd

XML Schema

Schema files that describe the format and constraints to apply to stored data.

.xslt

XML Style Sheet

Formatting rules to apply when displaying XML data.

The Files in a Web Application

When you build a Web Forms project, Visual Studio .NET compiles all of the source code into an executable assembly (DLL) and places that file in a /bin directory. The appearance portion of your application remains as .aspx and .html files. Figure 2-11 shows the files you deploy after you build your Web application.

figure 2-11 files in a web application

Figure 2-11. Files in a Web application

As shown in Figure 2-11, when a browser requests a page from the application, IIS starts the application s executable (Hello.dll) and generates a response. In this case, the response is to display the page defined in Hello.aspx. If an event, such as a button click, occurs on the page, it is sent back to the server, where it is handled by the application s DLL.

The following two lessons cover the details of how events are handled and where processing occurs.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Web Applications With Microsoft Visual Basic. Net and Microsoft V[.  .. ]0-315
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Web Applications With Microsoft Visual Basic. Net and Microsoft V[. .. ]0-315
ISBN: N/A
EAN: N/A
Year: 2003
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net