Special Edition Using Visual Basic.NET

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); } function Print() { window.focus(); if(window.print) { window.print(); window.setTimeout('window.close();',5000); } }
Team-Fly    

Special Edition Using Microsoft® Visual Basic® .NET

By Brian Siler, Jeff Spotts

Table of Contents
Chapter 3.  Creating Your First Web Application

When you request a Web page with your Internet browser, the end result is a document that appears on your screen. A document by itself is not much of an application in the traditional sense. However, when you include the activities going on behind the scenes that generate a Web page, the term Web application makes more sense. For example, when you click the Buy button at your favorite online store, a program may execute to validate your credit card number and other information in a customer database.

The major difference between a Web application, such as the online store, and a traditional Windows application is that most of the code resides on a Web server instead of your local PC. The Web server does all the hard work, and then serves the resulting Web page back to your browser. There are two major advantages to this centralized architecture:

  • Your code only exists in one place, so it can be updated easily.

  • Installation of your application by end users is not required. This is because the Web application can send standard Web syntax,such as the Hypertext Markup Language (HTML), which the user's browser already understands.

Understanding Web Application Projects

There are a lot of different ways you can use Visual Basic in a Web site. Some of the most common are as follows:

  • ASP.NET You can create Active Server Pages, which contain Visual Basic code that executes on the server.

  • Visual Basic Classes Classes that have been compiled into a DLL file can be installed on your Web server. These classes, which contain business logic and database functions, can be called from ASP.NET pages or exposed as Web services.

  • Client-Side Scripting VBScript, a subset of the Visual Basic language, can be used to execute code inside of the user's browser after the Web page has been delivered. This allows you to create a richer UI experience than can be achieved with standard HTML.

Each of the previous methods can be used independently (and, incidentally, without Visual Studio .NET) to create a functional Web application. However, a Visual Studio .NET Web Application project combines the previous elements together transparently.

Note

In addition to the methods listed here, you also can use WinForms on the Web, by using the Web as your data transfer backbone. For more information on this, see Chapter 17, "Using Active Server Pages.NET."

For more details on using classes and ASP.NET, p. 443

Connecting to Your Web Server

As we mentioned earlier, the content and programs of a Web application are stored on a Web server. Therefore, in order to develop Web applications, you will need to identify a Web server on your network. For the purposes of development and testing, this Web server can be the same computer on which you have installed Visual Studio.

Before creating a Web Application, you need to verify that a Web server is active and you know its address (or URL). The easiest way to see whether a Web server exists is to try to connect to it with Internet Explorer. Start Internet Explorer, type http://servername in the address box, and press Enter. If your Web server is up and running, some type of Web page should be displayed, as shown in Figure 3.1.

Figure 3.1. If you have not published any Web pages or changed the default configuration of your Web server, you may see a page similar to the one pictured here, which is a default page for Internet Information Server 5.0.

Note

If you want to use your current PC as the Web server, http://localhost is often defined as a valid Web address.

If you get a non-error response from your Web server, then you have identified a working address. If you get an error message, or are unable to determine the address, please consult the troubleshooting section at the end of this chapter.

Note

If you are using a separate Web server machine, it must have the Microsoft .NET Framework installed, which is included with Visual Studio. Please see the troubleshooting section at the end of this chapter for more details.

Creating a Web Application Project

Now that you have identified a Web server and verified it is running, we can begin the creation of the sample application. The first step is to start Visual Studio .NET and create a new Web Application project. To do this, first click the Create New Project link. Next, single-click the ASP.NET Web Application icon. You should see the dialog box pictured in Figure 3.2.

Figure 3.2. When you create a new ASP.NET Web Application project, Visual Studio will immediately connect to the Web server and copy several project files there.

In the name box, type LoanCalcApp, which is the name of our sample project. In the Location box, type your Web server's address.

Note

The Web server address plus the project name is the virtual directory where your Web application files will be stored on the Web server.

Next, click OK to create your new project. Visual Studio will connect to the Web server and create the new Web application. Your project will then be opened in Visual Studio for you to work with, as pictured in Figure 3.3.

Figure 3.3. Note that a Web form has no visible borders, as with a windows form.

Note

If the project does not open, but you get an error message, you may need to perform additional Web server configuration. Please see the section, "Troubleshooting Web Applications," at the end of this chapter for more information.

Setting the pageLayout Property

If you have followed the example in Chapter 2, "Creating Your First Windows Application," or designed forms in previous versions of Visual Basic, you know that you design the interface of an application by drawing and arranging objects on a form. When the form is presented to the user, the objects appear as you designed them, because their arrangement (or layout) is saved with the form.

Web forms work a little differently; they are presented to the user based on the Web browser's interpretation of instructions received from the Web server.

In support of the Web architecture, Visual Studio provides two different ways to specify the layout of Web forms: linear and grid-based. (The message in the middle of the Web form in Figure 3.3 indicates grid mode is selected.) Each layout mode has significant impact on the way the browser displays the Web form; this will be discussed later. For the sample project, we will use the default grid layout mode, which more closely parallels windows form design.

For more information on the pageLayout property, p.475


    Team-Fly    
    Top
     

    Категории