How to Test a Web Application
(Page 1 of 4 )
Welcome to the conclusion of a five-part article series on building web applications with ASP.NET. Now that we've built our web application, we need to test it. This article is excerpted from chapter two of
Murach's ASP.NET 3.5 Web Programming with VB 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774472).
The Visual Basic code for the Future Value form
Figure 2-17 presents the complete Visual Basic code for the code-behind file of the Future Value form. It consists of three event handlers that handle the Load event for the page and the Click events of the Calculate and Clear buttons. This code also includes a function procedure named FutureValue that is called by the event handler for the Click event of the Calculate button.
In this code, I’ve highlighted the two page properties that are commonly tested in the code for web forms. The first one is the IsPostBack property that’s used in the Page_Load procedure. If it is True, it means that the page is being posted back from the user. If it is False, it means that the page is being requested by the user for the first time.
As a result, the statements within the If statement in the Page_Load procedure are only executed if the page is being requested for the first time. In that case, the values 50 through 500 are added to the drop-down list. For all subsequent requests by that user, the IsPostBack property will be True so the values aren’t added to the drop-down list.
The other page property that’s commonly tested is the IsValid property. It’s useful when the user’s browser doesn’t support the script for the validation controls. In that case, the application has to rely on the validation that’s always done on the server. Then, if IsValid is True, it means that all of the input data is valid. But if IsValid is False, it means that one or more controls contain invalid input data so the processing shouldn’t be done.
In the btnCalculate_Click procedure, you can see how the IsValid test is used. If it isn’t True, the processing isn’t done. But otherwise, this procedure gets the years and interest rate values from the text boxes and converts them to monthly units. Then, it uses the SelectedValue property of the drop-down list to get the value of the selected item, which represents the investment amount. Last, it calls the FutureValue function to calculate the future value, uses the FormatCurrency method to format the future value, and puts the formatted value in the label of the form. When this procedure ends, the web form is sent back to the user’s browser.
With the exception of the IsPostBack and IsValid properties, this is all standard Visual Basic code so you shouldn’t have any trouble following it. But if you do, you can quickly upgrade your Visual Basic skills by getting our latest Visual Basic book.
The Visual Basic code for the Future Value form
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
For i As Integer = 50 To 500 Step 50
ddlMonthlyInvestment.Items.Add(i)
Next
End If
End Sub
Protected Sub btnCalculate_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim months As Integer
Dim interestRate, monthlyInvestment As Decimal
Dim futureValue As Decimal
If IsValid Then
months = txtYears.Text * 12
interestRate = txtInterestRate.Text / 12 / 100
monthlyInvestment = ddlMonthlyInvestment.SelectedValue
futureValue = Me.FutureValue(months, interestRate, _
monthlyInvestment)
lblFutureValue.Text = FormatCurrency(futureValue)
End If
End Sub
Private Function FutureValue(ByVal months As Integer, _
ByVal interestRate As Decimal, _
byVal monthlyInvestment As Decimal) As Decimal
For i As Integer = 1 To months
FutureValue = (FutureValue + monthlyInvestment) _
* (1 + interestRate)
Next
End Function
Protected Sub btnClear_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles btnClear.Click
ddlMonthlyInvestment.Text = 50
txtInterestRate.Text = ""
txtYears.Text = ""
lblFutureValue.Text = ""
End Sub
End Class
--------------------------------------------Figure 2-17 The Visual Basic code for the Future Value form
Next: How to test a web application >>
More ASP.NET Articles
More By Murach Publishing
|
This article is an excerpt from chapter two of Murach's ASP.NET 3.5 Web Programming with VB 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774472). Check it out today at your favorite bookstore. Buy this book now.
|
|