12:33 PM

Page 73


Creating Our First
ASP.NET Web Page
In the last two hours, we’ve spent quite a bit of time talking in very highlevel 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.



12:33 PM

Page 74


Hour 3

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
• Creating the design requirements for the financial calculator
• Creating the user interface
• Adding the needed Web controls to the ASP.NET Web page
• Writing the code for the ASP.NET Web page’s source code portion
• Testing the ASP.NET Web page

Specifying the Design Requirements
Throughout 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 steps—design, development, and testing—should 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:
• It lays down a road map for the software project. Having a road map allows us to
determine how much progress we’ve made at a given point, as well as how much we
have left to accomplish.
• The design requirements spell out precisely what the software will provide.
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.

The loan’s annual interest rate 3.NET Web Page 75 Without spending adequate time in the design requirements stage. “How much longer will this take. Deciding on the User Interface After describing the features that the application will have. and interest rate of the mortgage. So far I have just mentioned that we will be creating a financial calculator. For our financial calculator let’s build a loan calculator designed to determine the monthly payments for a fixed home mortgage.” or “How much progress have you made?” Additionally.ch03.06. three inputs are needed: 1. The amount of money being borrowed (the principal) 2. 3 .0672325438. duration. To determine the monthly payments required for a fixed mortgage. In a sentence: Our financial calculator will compute the monthly payment of a fixed mortgage when provided the amount. gives us the features of our financial calculator. however. For our financial calculator. your boss and client might wonder why a feature they thought was going to be present was not. the next stage in the design requirements phase is to create a user interface. 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. otherwise. along with these three inputs.qxd 5/5/03 12:33 PM Page 75 Creating Our First ASP. you would be unable to accurately answer your boss when he asks. agreeing on a list of feature requirements—a task typically performed during the design requirements stage—avoids any confusion at the conclusion of the project. the user interface is fairly straightforward and will exist on a single Web page. but let’s take the time to specifically define the features we want to provide. The duration of the loan—typically 15 or 30 years (the loan’s term) The output of our financial calculator. or UI for short. The user interface. Formulating the Features for Our Financial Calculator An important step in the design requirements process is to list the features you plan on providing in your application. is the means by which the user interacts with the application.

Figure 3. there is a button labeled Compute Monthly Cost that the user is instructed to click once having entered the required inputs. Note that the output shows how much money the mortgage will cost per month. The output of the financial calculator should show the mortgage’s monthly cost. Note the three textboxes for the three inputs.2 The monthly cost of the mortgage is shown. our users need to be able to do two things: enter the three inputs discussed earlier and see the result of the calculation. Additionally. .1 The user is asked to enter the three inputs. FIGURE 3.0672325438.qxd 5/5/03 12:33 PM 76 Page 76 Hour 3 Essentially.06.ch03.NET Web page financial calculator when first visited by the user. Figure 3.1 contains a screenshot of the ASP.2 contains a screenshot of the financial calculator after the user has entered the requested inputs and has clicked the Compute Monthly Cost button. FIGURE 3. These inputs can be entered via TextBox Web controls.

NET Web page precisely where we want the final output to appear.NET Web page.06. when clicked. launch the Web Matrix Project and create a new ASP.3 contains a screenshot of the Web Matrix Project with glyphs enabled. we will turn our attention to writing the needed source code to perform the financial computations. Therefore. To start creating our user interface.qxd 5/5/03 12:33 PM Page 77 Creating Our First ASP. we should place this Label Web control in the ASP. will perform the necessary computations. two yellow tags that mark the beginning and end of your form HTML control. This Label Web control will display the result of the calculation. As you can see from Figure 3.aspx.NET Web page. you should see. To construct this UI. Figure 3.NET Web page named FinancialCalculator. I have created the financial calculator so that the output appears below the input TextBoxes.2. Creating the User Interface Now that we’ve completed the design requirements phase and have decided what features our financial calculator will provide. To turn on glyphs.NET Web page. After creating the user interface. as well as a Button Web control that.NET Language option (the default). as well as how the interface will appear to the user. we’ll add a TextBox Web control for each of the three inputs. The first task is to create the user interface (or UI).ch03. in your Design tab.NET page. Before we add any content to the HTML portion of our ASP. we need to add a Label Web control to our ASP. first take a moment to turn on glyphs. which is considered the HTML portion of our ASP. With glyphs activated. go to the View menu and choose the Glyphs option. making certain to use the Visual Basic .0672325438. 3 . it’s time to actually start creating our ASP.NET Web Page 77 In order to display the output of our calculation. which are markers in the designer that indicate the location of HTML controls and Web controls.

NET Web page and instead see only Source and Preview tabs.0672325438.3 Turning on glyphs uses markers to display invisible HTML controls and Web controls.NET Programming Model. you have the Web Matrix Project in Preview Mode and need to change to Design Mode. . “Understanding the ASP. The <form> tag is inserted in an HTML control because it has the runat=”server” attribute.06. as can be seen by clicking the HTML tab (see Figure 3.qxd 5/5/03 78 12:33 PM Page 78 Hour 3 FIGURE 3.4). If you do not see a Design tab for your ASP.ch03. Refer back to the “Editing the HTML Content by Using the HTML Tab” section from Hour 2. 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.” for information on switching back to Design Mode.

4 The HTML portion of the ASP. as opposed to before or after the Web form. DropDownLists. CheckBoxes.06. The remainder of this book will refer to form HTML controls as either Web forms or server-side forms. which accepts three inputs from the user—it is required that the Web controls for user input (the TextBoxes. When creating an ASP. Then we can be sure that the controls we drop onto the designer from the Toolbox actually end up between the Web form’s start and end tags.NET Web Page 79 FIGURE 3. it is important that we turn on glyphs so that we can see the Web form’s start and finish.0672325438. 3 The form HTML control is commonly referred to as a Web form or server-side form. “Web Form Basics.qxd 5/5/03 12:33 PM Page 79 Creating Our First ASP.ch03. We will discuss why this needs to be done in Hour 9. .NET Web page contains a form HTML control. and so on) be placed within a Web form.” Because our user input Web controls need to be within the Web form.NET Web page that accepts user input—such as our financial calculator.

and then drag a TextBox control from the Toolbox and drop it into the designer after the “Mortgage Amount:” title. .6.5 Hit Enter to create some space between the Web form start and end tags. Because the first input is the amount of the mortgage. Create some space between the Web form’s start and end tags by hitting enter a few times.5. Take a moment to make sure your screen looks similar to the screenshot shown in Figure 3. let’s first create the title for the textbox we’re going to add.ch03. start by typing in this title: Mortgage Amount:. FIGURE 3.06. Next we want to add a TextBox Web control after this title.qxd 5/5/03 12:33 PM 80 Page 80 Hour 3 Adding the Three TextBox Web Controls Let’s start by adding the TextBox Web controls for our user’s three inputs. To accomplish this. make sure that the Web Controls tab from the Toolbox is selected. You should see something similar to Figure 3. 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. First make sure that you are in the Design tab. Before we drag a TextBox Web control to the designer.0672325438.

Specifically. Currently. If you do not use TextBox Web controls. the TextBox Web control we just added has its ID property set to TextBox1.6 At this point you should have a title and a single textbox. 3 When dragging and dropping the TextBox Web control from the Toolbox.NET Web Page 81 FIGURE 3. Therefore. not HTML textboxes. 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. it is very important that the Web Control tab be selected so that you are indeed dragging and dropping TextBox Web controls.06.0672325438. change the ID property to loanAmount. If the HTML Elements tab is selected on the Toolbox. .qxd 5/5/03 12:33 PM Page 81 Creating Our First ASP.ch03. you are dragging TextBox Web controls. let’s choose an ID value that is representative of the data found within the TextBox. be certain that when dragging and dropping TextBoxes onto the designer for this exercise. both inside the Web form tags. you’ll be placing standard HTML textboxes (textboxes without the runat=”server” attribute present).NET Web page. you will not be able to reference the values that the user entered in the TextBoxes in the source code portion of the ASP.

Feel free to change the font or the aesthetics of the HTML portion however you see fit. you might want to add a percent sign so that the user knows to enter this value as a percentage. add the third textbox. the ID property is denoted as (ID).qxd 82 5/5/03 12:33 PM Page 82 Hour 3 To change a Web control’s ID property. You might want to type in some text after each TextBox Web control to indicate the units that should be entered into the textbox. the mortgage’s interest rate. you might want to enter the word “years” after the “Mortgage Length” TextBox.7 shows the TextBox Web control titles in the standard font. Note that in the list of properties in the Properties pane. For example. Now let’s add the second textbox.7 contains a screenshot of the Design tab after all three input TextBox Web controls have been added. Finally.0672325438. Set this TextBox’s ID to mortgageLength. Add it just as we did the previous TextBox Web control by first creating a title for the TextBox. and then drag and drop a TextBox Web control after the title. Next drag and drop a TextBox Web control after this title and change the TextBox’s ID property to rate. which will load the Web control’s properties in the Properties window in the lower right-hand corner. Just be sure to have three TextBox Web controls inside of the Web form.ch03. the duration of the mortgage. The screenshot in Figure 3. Scroll to the top of the Properties pane until you see the ID property.06. . Figure 3. Start by adding the title Mortgage Length:. click the Web control in the designer. Type in the title Annual Interest Rate:. after the “Annual Interest Rate” textbox. Similarly. This is the property value that you should change.

7. it can perform the financial computation and return the results.NET Web page receives these user-entered values. This HTML markup. .aspx).NET Web Page.06.7 A screenshot of the Design tab. 3 Adding the Compute Monthly Cost Button After the user has entered inputs into the three TextBox Web controls.ch03. shown after all three TextBox Web controls have been added. when rendered by a browser.aspx ASP.NET Web page. the inputs entered by the user must be submitted back to our ASP.NET Web page (FinancialCalculator.0672325438. it converted the TextBox Web controls into HTML <input> tags. as shown in Figure 3. displays three textboxes. “Dissecting Our First ASP. In order for the calculation to take place. though. that when the ASP.NET Web Page 83 FIGURE 3. within it.qxd 5/5/03 12:33 PM Page 83 Creating Our First ASP. three <input> textbox tags. Once our ASP.NET Web page via their browsers. they are receiving HTML that contains a <form> tag and.” when the users visit the FinancialCalculator.NET engine executed the ASP. Realize. As we’ll discuss in much greater detail in the next hour. we want to be able to take that information and perform our financial calculation.

This will change the caption on your button to “Compute Monthly Cost. click the button and then in the Properties pane change the Text property from Button to Compute Monthly Cost.” Also. causes the form to be submitted. dropping it after the last input title and textbox.8.NET Web page. the user needs a button that.ch03. Take a moment to make sure that your screen looks similar to the screenshot in Figure 3. . FIGURE 3. while in the Properties pane. We can add such a button by adding a Button Web control to our ASP.” To change this. the button’s caption reads “Button.0672325438.06.” To add a Button Web control.qxd 5/5/03 12:33 PM 84 Page 84 Hour 3 In order for an HTML form to be submitted.8 A Button Web control has been added. change the button’s ID property—listed in the property pane as (ID)—from the default Button1 to performCalc. When dropping a Button Web control onto the designer. We will discuss the specifics involved with collecting and computing user input in Hour 9. “Web Form Basics. Then drag the Button Web control from the Toolbox onto the designer. first ensure that the Web Controls tab in the Toolbox is selected. when clicked.

simply drag and drop a Label Web control after the existing content in the designer. clear out the Label’s Text property. If you want the output to appear at the top of the Web page.” The Label Web control displays the value of its Text property. the Web page’s final result will appear wherever you place the Web control. To add the Label Web control.NET Web page. 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.NET Web page. Once you have added the Label Web control.ch03.9 A Label Web control has been added to the ASP. Figure 3.06.NET Web Page 85 Creating a Label Web Control for the Output The 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.9 is a screenshot of the designer with the Label Web control added. 3 . which is configurable via the Properties pane. in the Properties pane. drag and drop it from the Toolbox and onto the designer. FIGURE 3. first click the Label Web control so that its properties are loaded in the Properties pane. Therefore. To clear out a property value for the Label Web control.0672325438. Then. 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. you will see that it displays the message “Label. Because the Label Web control will display the output (the amount of money the mortgage costs per month). if you want the output to appear at the bottom of your ASP.qxd 5/5/03 12:33 PM Page 85 Creating Our First ASP. place it before the existing content in the designer.

the designer will show the Label Web control as its ID property. the Label Web control’s ID property is Label1.10 The Label Web control’s ID has been changed to results. click the HTML tab. To fully appreciate the amount of HTML markup the Web Matrix Project generated for us automatically.10 shows a screenshot of the designer after the Label Web control’s property has been changed to results. Currently. Completing the User Interface At this point we have added the vital pieces of the user interface. FIGURE 3. Go ahead and change the ID property of the Label Web control from Label1 to results.qxd 5/5/03 86 12:33 PM Page 86 Hour 3 Once you clear out the Label’s Text property. Figure 3. . 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. enclosed by brackets.ch03.0672325438. meaning that in the designer you should see the Label Web control displayed as: [Label1]. which should change the label’s display in the designer from [Label1] to [results].06.

perhaps a bold. This event handler. feel free to do so. all that remains is the source code.NET Web page. In the previous hour we looked at the Page_Load event handler. e As EventArgs) End Sub 3 . Therefore. because we do not want to run the calculation until the user has entered the loan amount.NET Web Page Now that we have completed the HTML portion of our ASP. you will be whisked to the Code tab. interest rate. which fires when the button is clicked. the event handler that we provide will be executed. Button Web controls have a Click event.ch03. what we want to do is write our own event handler and associate it with the Compute Monthly Cost button’s Click event. and duration. which you can include in your ASP. and has clicked the Compute Monthly Cost button.NET Web Page 87 If you want to add additional user interface elements at this time. then. we are ready to create the source code portion in the next section. Adding event handlers to a Button Web control’s Click event is quite easy to accomplish with the Web Matrix Project. whenever the Compute Monthly Cost button is clicked.NET Web page. All that remains. HTML markup and Web controls not used for user input may appear either within the Web form tags or outside of these tags.0672325438.06. This way. We will not be placing the source code to perform the monthly mortgage cost calculation in this event handler. where you should see the following source code already entered: Sub performCalc_Click(sender As Object.NET Web page’s source code portion. though. is executed each time the Web page is loaded. From the designer. simply double-click the Button whose Click event you would like to provide an event handler for. Writing the Source Code for the ASP. The only things that must be placed within the Web form tags are the Web controls that collect user input (the TextBoxes and Button). Now that we have created the HTML portion of the ASP. The source code will read the user’s inputs and perform the necessary calculations to arrive at the monthly cost for the mortgage.qxd 5/5/03 12:33 PM Page 87 Creating Our First ASP. Once you double-click the Button Web control. is to place the source code that performs the computation inside this event handler. centered title at the top of the Web page or a brief set of instructions for the user.

qxd 5/5/03 12:33 PM 88 Page 88 Hour 3 These two lines of code are the shell for the button’s Click event handler. it is said to have been instantiated. More generally. (Recall that after adding the Button Web control. The act of creating an object is often referred to as instantiation. we changed its ID from Button1 to performCalc. Both the TextBox and the Label classes have Text properties that indicate that Web control’s text content. the code to perform this calculation will appear within the performCalc_Click event handler.NET engine is executing an ASP. let’s take a step back and reexamine Web controls. methods. We will discuss how to find the properties. That is. and events for these classes in future hours. a topic we touched upon lightly in the previous hour. Before we look at the code to accomplish this. For example. Standard HTML markup is passed directly from the ASP.NET Framework. Web controls are handled quite differently from standard HTML elements. where buttonID is the value of the button’s ID property. whereas an object is a concrete instance. with Web controls. Recall from Hour 2’s discussion that when the ASP.ch03. Because we want to compute the monthly cost of the mortgage when the performCalc button is clicked. Each of these classes has various properties that describe the state of the Web control. The object is created from the class that corresponds to the specific Web control.NET Web page. When an object is created. a TextBox Web control has an object instantiated from the TextBox class. the TextBox class has a Size property that indicates how many columns the textbox has. an object is created that represents the Web control.0672325438. Recall that a class is an abstract blueprint.) Any code that you write between these two lines will be executed whenever the button is clicked.NET engine to the Web server without any translation. performCalc Reading the Values in the TextBox Web Controls In order to calculate the monthly cost of the mortgage. The classes that represent various Web controls are classes in the . . however. it is named buttonID_Click.06. Note that the event handler is named performCalc_Click. whereas a Label Web control has an object instantiated from the Label class. we must first be able to determine the values entered by the user into the three TextBox Web controls.

NET Web page’s source code section. Don’t worry if the syntax for retrieving an object’s property confuses you. we use the syntax loanAmount.) Keep in mind that the line numbers shown in Listing 3. Take a moment to enter the source code shown below into the performCalc Button’s Click event handler. Because loanAmount is the ID of the Mortgage Amount TextBox Web control.06.” The Complete Source Code Listing 3. it names the object the value of the Web control’s ID property.NET Variables and Operators. Because the Text property of the TextBox Web control contains the content of the textbox.1 The Computation Is Performed in the performCalc Button’s Click Event Handler 1: Sub performCalc_Click(sender As Object. we could use the following line of code: loanAmount.Text. the object created representing this Web control is named loanAmount. To retrieve the Text property of the loanAmount object. e As EventArgs) 2: ‘Specify constant values 3: Const INTEREST_CALCS_PER_YEAR as Integer = 12 4: Const PAYMENTS_PER_YEAR as Integer = 12 5: 6: ‘Create variables to hold the values entered by the user 7: Dim P as Double = loanAmount. we can reference this property in the Compute Monthly Cost button’s Click event handler to determine the value entered by the user into each textbox.Text continues 3 .1 contains the complete source code for our ASP.qxd 5/5/03 12:33 PM Page 89 Creating Our First ASP. to determine the value entered into the Mortgage Amount textbox.Text When the ASP.ch03. We will be discussing the syntax and semantics of Visual Basic .NET Web Page 89 The primary benefit of Web controls is that their properties can be accessed in the ASP.NET Web page. “Visual Basic . For example. (You should do this from the Code or All tab.0672325438. The line numbers are present in the code listing only to help reference specific lines of the listing when discussing the code.NET in greater detail in Hour 5.NET engine creates an object for the Web control. LISTING 3.1 should not be typed in as well.

06.html.Exp(INTEREST_CALCS_PER_YEAR * Math. even if there are parts of it you don’t understand.1 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: Continued Dim r as Double = rate.Text / 100 Dim t as Double = mortgageLength.qxd 5/5/03 12:33 PM 90 Page 90 Hour 3 LISTING 3.1) * payPeriods 22: 23: ‘Now. though.-payPeriods)) 28: 29: 30: ‘Now. simply enter the code as is.org/faqs/sci-math-faq/compoundInterest/. The mathematical equations used to calculate the monthly interest cost can be found at http://www.NET Web page in the next two hours. For now.1 has you hopelessly lost and confused.Exp(Math. .Pow(intPerMonth+1.Text = “Your mortgage payment per month is $” & costPerMonth 32: End Sub An in-depth discussion of the code in Listing 3.ch03.NET Web page quickly. display the results in the results Label Web control 31: results.faqs.edu/faculty/Stefan_Waner/RealWorld/Summary10.hofstra. compute the total cost of the loan 24: Dim intPerMonth as Double = intPerPayment / PAYMENTS_PER_YEAR 25: 26: Dim costPerMonth as Double 27: costPerMonth = P * intPerMonth/(1-Math. The point of this hour is to get you creating a useful ASP. If the source code in Listing 3.0672325438.Log(annualRate+1)/payPeriods) . In these three lines we are reading the values of the three TextBox Web controls and assigning the values to variables.1 will have to wait until the next hour. A more in-depth discussion of these formulas can be found at http://people. One thing to pay attention to.Text Dim ratePerPeriod as Double ratePerPeriod = r/INTEREST_CALCS_PER_YEAR Dim payPeriods as Integer payPeriods = t * PAYMENTS_PER_YEAR Dim annualRate as Double annualRate = Math. is in lines 7 through 9. we will take the time needed to dissect the HTML and source code portions of this ASP. don’t worry.1 Dim intPerPayment as Double intPerPayment = (Math.Log(1+ratePerPeriod)) .

12. as shown in Figure 3. the monthly cost is displayed beneath the textboxes and button. Next view the ASP.NET Web Page 91 Testing the Financial Calculator Now that we have completed the HTML and source code portions of our ASP.1. 3 Now go ahead and enter some values into the textboxes and then click the Compute Monthly Cost button.0672325438. FIGURE 3. you should see three empty textboxes and the Compute Monthly Cost button. .06.NET page is first visited.12 The output of the financial calculator is displayed when the Button is clicked. FIGURE 3. as shown in Figure 3.NET Web page since entering the source code in Listing 3.NET Web page through your browser. When first visiting the page. it’s time to test.qxd 5/5/03 12:33 PM Page 91 Creating Our First ASP.NET Web page. three textboxes await user input.11 When the ASP. When this button is clicked.11. First make sure that you have saved the ASP.ch03.

NET’s input validation features in Hour 12. input validation is incredibly easy with ASP. “Validating User Input with Validation Controls.NET. what will happen if the user enters into the Mortgage Length textbox a value of “Jisun”? Obviously. as shown in Figure 3. Not only is it important to plan how the ASP.13 are an eyesore. explaining that the input is not in the right form.0672325438.NET Web pages should work. FIGURE 3. this is not a valid number of years. but it is also important to plan on how to ASP. Entering such an erroneous value will cause a run-time error.NET Web page prior to creating the page.13.” Earlier in this hour we discussed the importance of planning the user interface and functionality of an ASP. We’ll examine ASP.NET Web page should behave when things don’t necessarily go according to plan. Errors such as those shown in Figure 3. it would be better to display a simple error message next to the textbox(es) with erroneous input.qxd 5/5/03 12:33 PM 92 Page 92 Hour 3 Testing Erroneous Input Part of testing is not only testing expected inputs but also testing unexpected ones. The process of ensuring that user input is in the correct format is known as input validation.13 A run-time error will occur if the input is not in proper format. Rather than displaying such error messages when the user enters erroneous input.06.ch03. Fortunately. . For example.

there are no HTML controls in the Web Matrix Project’s Toolbox. Finally.NET Web page.NET Web page by visiting it with a Web browser and entering some values for the three textboxes. That is.NET Web page and consider using HTML controls only when an example explicitly mentions their use. you would have to enter the markup for the HTML controls by hand in the HTML tab. We started by outlining the features we wanted to include in our ASP. In this hour we did not spend much time discussing the source code or the specifics of the Button Web control’s Click event and corresponding event handler. The code to perform the calculation was inserted in an event handler for the Compute Monthly Cost button’s Click event.NET Web Page 93 Summary In this hour we saw how to create our first useful ASP. Rather. Next we implemented the user interface by completing the HTML portion of the ASP.NET Web page? A Yes. We accomplished this 3 . Q&A Q Can I use HTML controls instead of Web controls for the textboxes in the HTML portion of the ASP.06. but I would advise against it. We will touch upon these issues in detail in the next two hours. Q How do I associate “event code” with a Web control that I’ve placed on a Web Form? A In this hour we saw how to have the Button Web control’s Click event associated with an event handler provided in the source code section. in large part because the Web Matrix Project’s Toolbox does not contain support for dragging and dropping HTML controls. it was simply a matter of typing in the textbox labels and dragging and dropping the needed TextBox. Using the Web Matrix Project’s WYSIWYG editor. we tested the ASP. We then briefly discussed what the user interface should look like. This had the effect of having the entered code executed whenever the user clicked the Compute Monthly Cost button. Button. we will be using Web controls extensively throughout this book. and Label Web controls.0672325438. the source code portion was entered.ch03. so I would encourage you to familiarize yourself with adding Web controls to an ASP.qxd 5/5/03 12:33 PM Page 93 Creating Our First ASP. just Web controls and HTML elements.NET Web page.NET Web page. Additionally. After the HTML portion. including the output and needed inputs.

interest rate. (Note that the Button Web control’s default event is the Click event. When the page is visited for the first time by the user. How do you add an event handler for a Button Web control’s Click event with the Web Matrix Project? 6. Why did we add a Label Web control to our ASP. an event handler is created for this default event.) Adding an event handler for an event other than a Web control’s default event involves a more thorough discussion than we are ready for at this point. When using a TextBox Web control. How can one add a TextBox Web control to an ASP. we will get an error. and duration.NET Web page using the Web Matrix Project? 3.0672325438. What will the ASP. Therefore.NET Web page’s output be if the user enters invalid characters into the textboxes—for example.NET Web page is requested.06. Realize that. Workshop Quiz 1. We’ll examine adding event handlers for events other than the Web control’s default event in the next hour. the user has yet to enter the loan principal. behind the scenes. the Web Matrix Project is performing a number of steps when you double-click the Button. in attempting to compute the calculation. the user enters “Scott”? 5. if under the Mortgage Amount textbox. Each Web control has a default event.ch03. Why is the design requirements phase of software development an important one? 2. what property is referenced to determine the value entered by the user? . When the Web control is double-clicked in the designer.NET Web page’s HTML portion? 4.qxd 5/5/03 12:33 PM 94 Page 94 Hour 3 by simply double-clicking the Button Web control in the designer. the source code for the calculation is placed in the button’s Click event handler. Q What would happen if I placed the financial calculation code in the Page_Load event instead of the button Click event handler? A Recall that the source code in the Page_Load event handler executes every time the ASP. Because we want to perform the calculation only after the user has provided the required inputs.

4. we have more flexibility over where the output appears in the HTML portion.NET Web page will need to have two TextBox Web controls. Without using a label.NET Web page’s source code portion. The Button Web control should have its Text property set to Click Me. and a Label Web control.Text Exercises 1.NET Web page’s HTML portion to indicate where the output of the financial calculator would appear. It is an important stage because by enumerating the features.qxd 5/5/03 12:33 PM Page 95 Creating Our First ASP. A Label Web control was added to the ASP. and a Label Web control. then. 6. we could output the results using only Response. we can use TextBoxID.NET Web page will display a message whose content depends on the user’s age. 2. Set the TextBox Web controls’ ID properties to name and age.NET Web page with TextBox Web controls. 3. you—and your boss and client—can easily determine the current progress of the project. The Text property contains the value entered by the user. the ASP.ch03. To add a TextBox Web control.NET Web page that will prompt the user for his name and age. The design requirements phase outlines the specific features for the software project and also outlines the user interface. a Button Web control. If the user provides invalid input. Recall from the previous hour that this would have the effect of omitting the results before the HTML portion of the ASP. To add an event handler for a Button Web control’s Click event. 3 . Using this knowledge. To reference this property in an ASP. Furthermore.06.NET Web page. By using a Label Web control. Set the Label Web control’s ID property to results and clear out its Text property.0672325438. You will then need to create an event handler for the Button Web control’s Click event—recall that this is accomplished by simply double-clicking the Button in the designer. Once the user provides this information and clicks the submit button. simply double-click the button that you wish to add an event handler for. This ASP. let’s create an ASP. a Button Web control. simply click the TextBox Web control from the Toolbox and drag it onto the designer. 5.NET Web Page 95 Answers 1.Write() statements. a run-time error will occur. In this hour we saw how to use the Web Matrix Project to create an ASP. there is no ambiguity as to what features should and should not be included.

Text & “.Text = name.Text >= 21 AND age. 2. take a moment to enhance the user interface of the FinancialCalculator.Text < 21 then results. include displaying the TextBox Web control titles in a more appealing font and adding some text at the top of the Web page explaining the purpose of the financial calculator.Text < 40 then results.Text >= 40 then results. you are a youngster!” End If If age.06.Text = name.ch03. in the Click event handler. you are an adult.” End If If age. The code for this will look like If age.Text & “. of many possible.Text = name. save the ASP.aspx Web page we created in this hour.NET Web page and test it by visiting it through a browser. A couple suggested enhancements.qxd 96 5/5/03 12:33 PM Page 96 Hour 3 Now. based on the user’s age.Text & “.0672325438. you need to determine what message to display. . For more practice with the Web Matrix Project. you are over the hill!” End If Once you have entered the preceding source code into the Button Web control’s Click event handler.