Now 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 the user interface, launch Visual Web Developer and create a new ASP.NET website on your computer's file system, just as we examined in the preceding hour. As we've seen, this will create a website with an ASP.NET page named Default.aspx. For practice, go ahead and delete this ASP.NET page and add a new page named FinancialCalculator.aspx. When you're creating a new ASP.NET page, remember that the item type to select is Web Form; also be sure to choose Visual Basic as the language and check the Place Source Code in a Separate File check box. Adding the Three TextBox Web ControlsLet's start by adding the TextBox Web controls for our user's three inputs. While we've yet to examine using the TextBox Web control in any of our previous examples, it's fairly straightforward. Just like with the Image and Label Web controls we added to our page in Hour 2, "Understanding the ASP.NET Programming Model," adding a TextBox Web control to a page is no different. Simply drag the TextBox from the Toolbox onto the page, either from the Design view or Source view. After adding the TextBox to the page, you can configure its properties through the Properties window (or by typing them in directly in the Source view, if you prefer).
Before we drag a TextBox Web control onto the page, 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 in the FinancialCalculator.aspx page's Design view: Principal 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; then drag a TextBox control from the Toolbox and drop it into the page after the Principal Amount: title. Take a moment to make sure your screen looks similar to Figure 4.3. Figure 4.3. At this point you should have a title and a single TextBox.
Currently, the TextBox Web control we just added has its ID property set to TextBox1. Because we will later need to programmatically refer to this ID in order to determine the value of the beginning mortgage principal 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:. Then drag and drop a TextBox Web control after the title. Set this TextBox's ID to mortgageLength.
Figure 4.4 shows the Design view after all three input TextBox Web controls have been added.
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. As we discussed in Hour 1, "Getting Started with ASP.NET 2.0," though, there is a temporal, physical, and logical disconnect between the clientthe end user's web browserand the serverthe web server software that renders the ASP.NET pages. When the user visits the FinancialCalculator.aspx ASP.NET web page via her browser, she is receiving HTML that contains the user interface we created from the Web controls and static HTML. Once the user's browser has received our ASP.NET page's HTML markup, there's no communication between the client and the server until the client explicitly makes a request back to the web server. Therefore, 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. For the client to post the inputs entered by the end user back to the web server, an HTML <form> is used. This process typically commences when a submit button is clicked. We can add such a button by adding a Button Web control to our ASP.NET web page.
To add a Button Web control, drag the Button Web control from the Toolbox onto the page, dropping it after the last input title and TextBox. When you add a Button Web control, the Button's caption reads "Button." To change this, click on the Button, and then in the Properties window, 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 window, change the Button's ID propertylisted in the Property window as (ID)from the default Button1 to performCalc. Take a moment to make sure that your screen looks similar to Figure 4.5. Figure 4.5. A Button Web control has been added.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 be used 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. After 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 window. Figure 4.6 shows the designer with the Label Web control added. Figure 4.6. A Label Web control has been added to the ASP.NET web page.Because we don't want this Label to display any content until the user has entered 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 on 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's value by clicking it and pressing Backspace until all of the characters have been erased. After 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 4.7 shows the designer after the Label Web control's property has been changed to results. Figure 4.7. The Label Web control's ID has been changed to results.Completing the User InterfaceAt this point we have added the vital pieces of the user interface. 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 we will tackle this task. |