To get started, let's create a web application named WebNorthWind. Open Visual Studio 2005. Click on New Web Site and in the drop-down menus, choose FileSystem, filling in an appropriate file location. Make sure the language is set to Visual Basic, as shown in Figure 8-2. Visual Studio 2005 will create a filesystem-based web site (you will not find the web site listed under IIS Management) and will create a file, Default.aspx, that represents the first ASP.NET form. The editor will open, and the web Toolbox will be visible (if Figure 8-2. Creating a new web sitenot, you can make it visible through the View window). The toolbox, like all windows, can be "pinned" in place by clicking on the thumbtack. Depending on how you've configured your system, you'll probably find yourself in the Source view, with a tabbed window allowing you to switch to WYSIWYG (What You See Is What You Get) Design view, as shown in Figure 8-3. Figure 8-3. Web development editorI've circled and numbered six areas of this screen:
Visual Studio creates a folder named WebNorthWind in the directory you've indicated, and within that directory it creates your Default.aspx page (for the User interface), Default.aspx.vb (for your code) and an App_Data directory (currently empty but often used to hold mdb files or other data-specific files).
Figure 8-6. Setting the project location8.2.1. Code-Behind FilesLet's take a closer look at the .aspx and code-behind files that Visual Studio creates. Start by renaming Default.aspx to Welcome.aspx. To do this, click on the name in the Solution explorer and rename the file.
Rename the class, which you do by right-clicking on the .aspx page and choosing View Code in the code page. Rename the class Welcome_aspx. You'll see a small line next to the name. Click on it and you'll open the smart tag that allows you to rename the class wherever it is used. Rename Default_aspx as Welcome_aspx, and Visual Studio will do the work of ensuring that every occurrence of Default_aspx is replaced with its new name, as shown in Figure 8-7. Figure 8-7. Renaming the classUnfortunately, the name of the class is not changed in the page directive in Welcome.aspx, so go back to the Welcome.aspx file and change the page directive's Inherits attribute to Welcome_aspx. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Welcome.aspx.vb" Inherits="Welcome_aspx" %> Within the HTML view of Welcome.aspx, you see that a form has been specified in the body of the page using the standard HTML form tag: <form runat="server"> ASP.NET assumes that you need at least one form to manage the user interaction, and creates one when you open a project. The attribute runat="server" is the key to the server-side magic. Any tag that includes this attribute is considered a server-side control to be executed by the ASP.NET framework on the server. Within the form, Visual Studio has added div tags to facilitate placing your controls and text. 8.2.2. Put a Toe in the WaterHaving created an empty web form, the first thing you might want to do is add some text to the page. By switching to Source view, you can add script and HTML directly to the file (just as you could with classic ASP.) Adding the following line to the <body> segment of the HTML page will cause it to display a greeting and the current local time: Hello World ! It is now <% = DateTime.Now.ToString( ) %> The <% and %> marks indicate that code falls between them (in this case, Visual Basic 2005). The = sign immediately following the opening tag causes ASP.NET to display the value, just like a call to Response.Write. You could just as easily write the line as: Hello World! It is now <% Response.Write(DateTime.Now.ToString( ))%> Run the page by pressing F5. Visual Studio 2005 will notice that you have not enabled debugging for this application, and a dialog box will appear, offering to enable debugging for you, as shown in Figure 8-8. Click OK. You should see the string printed to the browser, as in Figure 8-9. Figure 8-8. Enabling debuggingFigure 8-9. Hello World from ASP.NET |