Now that we have completed the HTML portion of our ASP.NET web page, all that remains is the source code. The source code will read the user's inputs and perform the necessary calculations to arrive at the monthly cost for the mortgage. In Hour 2 we looked at the page's Load event and the corresponding Page_Load event handler. This event handler, which you can include in your ASP.NET web page's source code portion, is executed each time the web page is loaded. We will not be placing the source code to perform the monthly mortgage cost calculation in this event handler, though, because we do not want to run the calculation until the user has entered the principal, interest rate, and duration values and has clicked the Compute Monthly Cost button. Button Web controls have a Click event, which fires when the button is clicked. Therefore, what we want to do is create an event handler that is associated with the Compute Monthly Cost button's Click event. This way, whenever the Compute Monthly Cost button is clicked, the event handler that we provide will be executed. When we have this event handler, all that will remain will be to write the source code that performs the computation inside this event handler. Creating an event handler for a Button Web control's Click event is remarkably easy to accomplish with Visual Web Developer. The quickest way is to go to the Design view and simply double-click the Button Web control. This will automatically create the necessary event handler and whisk you to the page's source code portion, where you should see the following source code:
Note that the event handler is named performCalc_Click. More generically, Visual Web Developer names it buttonID_Click, where buttonID is the value of the Button's ID property. (Recall that after adding the Button Web control, we changed its ID from Button1 to performCalc.) Another way to create this same event handler is to go to the source code portion. At the top you'll find two drop-down lists. From the left drop-down list, select the Button Web control (performCalc) and then, from the right drop-down list, select the Click event. After you select the Click option, an event handler will be automatically created for the performCalc Button's Click event with the same code had you double-clicked the Button in the Design view. Any code that you write within the event handler will be executed whenever the performCalc button is clicked. Because we want to compute the monthly cost of the mortgage when the performCalc button is clicked, the code to perform this calculation will appear within the performCalc_Click event handler. Reading the Values in the TextBox Web ControlsTo calculate the monthly cost of the mortgage, we must first be able to determine the values the user entered into the three TextBox Web controls. Before we look at the code to accomplish this, let's take a step back and reexamine Web controls, a topic we touched upon lightly in Hour 2. Recall that when the ASP.NET engine is executing an ASP.NET web page, Web controls are handled quite differently from standard HTML elements. With static HTML content, the markup is passed directly from the ASP.NET engine to the web server without any translation; with Web controls, however, the Web control's syntax is rendered into a standard HTML element. Specifically, what's happening behind the scenes is that an object is created that represents the Web control. The object created is created from the class that corresponds to the specific Web control. That is, a TextBox Web control has an object instantiated from the TextBox class, whereas a Label Web control has an object instantiated from the Label class.
Each of these Web control classes has various properties that describe the state of the Web control. For example, the TextBox class has a Columns property that indicates how many columns the TextBox has. Both the TextBox and Label classes have Text properties that indicate that Web control's text content.
The primary benefit of Web controls is that their properties can be accessed in the ASP.NET web page's source code section. Because the Text property of the TextBox Web control contains the content of the TextBox, we can reference this property in the Compute Monthly Cost button's Click event handler to determine the value the user entered into each TextBox. For example, to determine the value entered into the Mortgage Amount TextBox, we could use the following line of code: loanAmount.Text When the ASP.NET engine creates an object for the Web control, it names the object the value of the Web control's ID property. Because loanAmount is the ID of the Mortgage Amount TextBox Web control, the object created representing this Web control is named loanAmount. To retrieve the Text property of the loanAmount object, we use the syntax loanAmount.Text.
The Complete Source CodeListing 4.1 contains the complete source code for our ASP.NET web page. Visual Web Developer has already written some of the codethe class declaration and the event handlerfor you. You will, however, need to enter the code between lines 4 and 35 into the performCalc Button's Click event handler.
Listing 4.1. The Computation Is Performed in the performCalc Button's Click Event Handler
An in-depth discussion of the code in Listing 4.1 is provided toward the end of this hour in the "Examining the Source Code" section. For now, simply enter the code as-is, even if there are parts of it you don't understand. One thing to pay attention to, though, is in lines 10 through 12. In these three lines we are reading the values of the three TextBox Web controls and assigning the values to variables.
|