![]() |
|
|
Published: Friday, June 27, 2003 Chapter 3: Creating Our First ASP.NET Web Page By Scott Mitchell Chapter 3: Creating Our First ASP.NET Web PageIn the last two hours, we've spent quite a bit of time talking in very high-level terms about ASP.NET Web pages and the ASP.NET programming model. We've looked at how to configure our computer to serve ASP.NET Web pages, and we've looked at the role of the Web server. We've examined the HTML and source code portions of an ASP.NET Web page and looked at HTML controls and Web controls. We've created some very simple ASP.NET Web pages and have seen how to use the Web Matrix Project to create these pages. In this hour we turn from these high-level discussions to actually building a useful ASP.NET Web page that illustrates the concepts discussed in the last two hours. Specifically, we'll be creating an ASP.NET Web page that serves as a financial calculator. This hour will focus on building the ASP.NET Web page, with only a light discussion of the source code and Web controls used. In the next hour, however, we will look at the ASP.NET Web page created in this hour in much more depth. Fire up the Web Matrix Project and get ready to start creating your first practical ASP.NET Web page! In this hour we will cover
Specifying the Design RequirementsThroughout this book we will be creating a number of ASP.NET Web pages, which involves creating both the ASP.NET Web page's HTML and its source code. When writing any piece of software, whether a Windows desktop application or a dynamic Web page, there are a number of development stages. First and foremost we need to decide what the purpose of the software is, along with what features and functionality the software should provide. After this we must sit down and actually write the software. Finally, we need to test the software and fix any bugs or errors that arise. These three stepsdesign, development, and testingshould always be performed when creating an ASP.NET Web page, but too frequently, developers jump straight to the coding task without spending enough time in the planning stage. This initial planning stage, sometimes called the design requirements stage, is vital for the following reasons:
To get into the habit, we will spend a bit of time discussing what features will be present and what user interface will be employed in the ASP.NET Web page we will be creating in this hour.
Formulating the Features for Our Financial CalculatorAn important step in the design requirements process is to list the features you plan on providing in your application. So far I have just mentioned that we will be creating a financial calculator, but let's take the time to specifically define the features we want to provide. For our financial calculator let's build a loan calculator designed to determine the monthly payments for a fixed home mortgage. To determine the monthly payments required for a fixed mortgage, three inputs are needed:
The output of our financial calculator, along with these three inputs, gives us the features of our financial calculator. In a sentence: Our financial calculator will compute the monthly payment of a fixed mortgage when provided the amount, duration, and interest rate of the mortgage. Deciding on the User InterfaceAfter describing the features that the application will have, the next stage in the design requirements phase is to create a user interface. The user interface, or UI for short, is the means by which the user interacts with the application. How will the user enter these inputs? How will the results be displayed? With large applications the user interface portion of the design requirements phase can take quite a while and be very involved. For our financial calculator, however, the user interface is fairly straightforward and will exist on a single Web page. Essentially, our users need to be able to do two things: enter the three inputs discussed earlier and see the result of the calculation. These inputs can be entered via TextBox Web controls. The output of the financial calculator should show the mortgage's monthly cost. Figure 3.1 contains a screenshot of the ASP.NET Web page financial calculator when first visited by the user. Note the three textboxes for the three inputs. Additionally, there is a button labeled Compute Monthly Cost that the user is instructed to click once having entered the required inputs. Figure 3.1 Figure 3.2 contains a screenshot of the financial calculator after the user has entered the requested inputs and has clicked the Compute Monthly Cost button. Note that the output shows how much money the mortgage will cost per month. Figure 3.2 In order to display the output of our calculation, we need to add a Label Web control to our ASP.NET page. This Label Web control will display the result of the calculation. Therefore, we should place this Label Web control in the ASP.NET Web page precisely where we want the final output to appear. As you can see from Figure 3.2, I have created the financial calculator so that the output appears below the input TextBoxes. Creating the User InterfaceNow that we've completed the design requirements phase and have decided what features our financial calculator will provide, as well as how the interface will appear to the user, it's time to actually start creating our ASP.NET Web page. The first task is to create the user interface (or UI), which is considered the HTML portion of our ASP.NET Web page. To construct this UI, we'll add a TextBox Web control for each of the three inputs, as well as a Button Web control that, when clicked, will perform the necessary computations.
To start creating our user interface, launch the Web Matrix Project and create a new ASP.NET Web page named FinancialCalculator.aspx, making certain to use the Visual Basic .NET Language option (the default). Before we add any content to the HTML portion of our ASP.NET Web page, first take a moment to turn on glyphs, which are markers in the designer that indicate the location of HTML controls and Web controls. To turn on glyphs, go to the View menu and choose the Glyphs option. With glyphs activated, you should see, in your Design tab, two yellow tags that mark the beginning and end of your form HTML control. Figure 3.3 contains a screenshot of the Web Matrix Project with glyphs enabled. Figure 3.3
Recall from our discussion in the previous hour that the Web Matrix Project automatically inserts a form HTML control in the HTML portion of your ASP.NET Web pages. The <form> tag is inserted in an HTML control because it has the runat="server" attribute, as can be seen by clicking the HTML tab (see Figure 3.4). Figure 3.4
When creating an ASP.NET Web page that accepts user inputsuch as our financial calculator, which accepts three inputs from the userit is required that the Web controls for user input (the TextBoxes, DropDownLists, CheckBoxes, and so on) be placed within a Web form. We will discuss why this needs to be done in Hour 9, "Web Form Basics."
Adding the Three TextBox Web ControlsLet's start by adding the TextBox Web controls for our user's three inputs. First make sure that you are in the Design tab. Next place your mouse cursor between the Web form glyphs and click the left mouse button so that the designer receives focus and there is a flashing cursor between the Web form glyphs. Create some space between the Web form's start and end tags by hitting enter a few times. You should see something similar to Figure 3.5. Figure 3.5 Before we drag a TextBox Web control to the designer, let's first create the title for the textbox we're going to add. Because the first input is the amount of the mortgage, start by typing in this title: Mortgage Amount:. Next we want to add a TextBox Web control after this title. To accomplish this, make sure that the Web Controls tab from the Toolbox is selected, and then drag a TextBox control from the Toolbox and drop it into the designer after the "Mortgage Amount:" title. Take a moment to make sure your screen looks similar to the screenshot shown in Figure 3.6. Figure 3.6
Currently, the TextBox Web control we just added has its ID property set to TextBox1. Because we will later need to refer to this ID in order to determine the value of the beginning retirement balance entered by the user, let's choose an ID value that is representative of the data found within the TextBox. Specifically, change the ID property to loanAmount.
Now let's add the second textbox, the mortgage's interest rate. Add it just as we did the previous TextBox Web control by first creating a title for the TextBox. Type in the title Annual Interest Rate:. Next drag and drop a TextBox Web control after this title and change the TextBox's ID property to rate. Finally, add the third textbox, the duration of the mortgage. Start by adding the title Mortgage Length:, and then drag and drop a TextBox Web control after the title. Set this TextBox's ID to mortgageLength.
Figure 3.7 contains a screenshot of the Design tab after all three input TextBox Web controls have been added.
Figure 3.7 Adding the Compute Monthly Cost ButtonAfter the user has entered inputs into the three TextBox Web controls, we want to be able to take that information and perform our financial calculation. Realize, though, that when the ASP.NET engine executed the ASP.NET Web page, it converted the TextBox Web controls into HTML <input> tags. As we'll discuss in much greater detail in the next hour, "Dissecting Our First ASP.NET Web Page," when the users visit the FinancialCalculator.aspx ASP.NET Web page via their browsers, they are receiving HTML that contains a <form> tag and, within it, three <input> textbox tags. This HTML markup, when rendered by a browser, displays three textboxes, as shown in Figure 3.7. In order for the calculation to take place, the inputs entered by the user must be submitted back to our ASP.NET Web page (FinancialCalculator.aspx). Once our ASP.NET Web page receives these user-entered values, it can perform the financial computation and return the results. In order for an HTML form to be submitted, the user needs a button that, when clicked, causes the form to be submitted. We can add such a button by adding a Button Web control to our ASP.NET Web page.
To add a Button Web control, first ensure that the Web Controls tab in the Toolbox is selected. Then drag the Button Web control from the Toolbox onto the designer, dropping it after the last input title and textbox. When dropping a Button Web control onto the designer, the button's caption reads "Button." To change this, click the button and then in the Properties pane change the Text property from Button to Compute Monthly Cost. This will change the caption on your button to "Compute Monthly Cost." Also, while in the Properties pane, change the button's ID propertylisted in the property pane as (ID)from the default Button1 to performCalc. Take a moment to make sure that your screen looks similar to the screenshot in Figure 3.8. Figure 3.8 Creating a Label Web Control for the OutputThe final piece we need to add to our user interface is a Label Web control that will to display the output of our financial calculation. Because the Label Web control will display the output (the amount of money the mortgage costs per month), the Web page's final result will appear wherever you place the Web control. Therefore, if you want the output to appear at the bottom of your ASP.NET Web page, simply drag and drop a Label Web control after the existing content in the designer. If you want the output to appear at the top of the Web page, place it before the existing content in the designer. To add the Label Web control, drag and drop it from the Toolbox and onto the designer. Once you have added the Label Web control, you will see that it displays the message "Label." The Label Web control displays the value of its Text property, which is configurable via the Properties pane. Figure 3.9 is a screenshot of the designer with the Label Web control added. Figure 3.9 Because we don't want this label to display any content until the user has entered their three inputs and the calculation has been performed, clear out the Label's Text property. To clear out a property value for the Label Web control, first click the Label Web control so that its properties are loaded in the Properties pane. Then, in the Properties pane, locate the Text property and erase the Text property value by clicking the Text property's value and hitting backspace until all of the characters have been erased. Once you clear out the Label's Text property, the designer will show the Label Web control as its ID property, enclosed by brackets. Currently, the Label Web control's ID property is Label1, meaning that in the designer you should see the Label Web control displayed as: [Label1]. Go ahead and change the ID property of the Label Web control from Label1 to results, which should change the label's display in the designer from [Label1] to [results]. Figure 3.10 shows a screenshot of the designer after the Label Web control's property has been changed to results. Figure 3.10 Completing the User InterfaceAt this point we have added the vital pieces of the user interface. This was accomplished using the Web Matrix Project's WYSIWYG editor in a fraction of the time it would have taken to enter the HTML markup and Web control syntax manually.
If you want to add additional user interface elements at this time, perhaps a bold, centered title at the top of the Web page or a brief set of instructions for the user, feel free to do so. Now that we have created the HTML portion of the ASP.NET Web page, we are ready to create the source code portion in the next section.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
![]() |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||