'Program:
Chapter 9 handsOn
'Programmer:
Bradley Shedd
'Date:
Aug 2006
'Description: A web site to calculate
the extended price for books sold,
'
a discount, and the discounted amount. Calculates and displays
'
the total discounts.
'
Uses validator controls for input validation.
'Folder:
Ch09HandsOn
Partial Class
_Default
Inherits System.Web.UI.Page
Private discountTotalDecimal
As Decimal
Const DISCOUNT_RATE_Decimal
As Decimal =
0.15D
Protected Sub
submitButton_Click(ByVal sender
As Object,
ByVal e As
System.EventArgs) Handles submitButton.Click
Dim quantityInteger
As Integer
Dim priceDecimal, extendedPriceDecimal
As Decimal
Dim discountDecimal,
discountedPriceDecimal As
Decimal
With Me
.errorMessageLabel.Text = String.Empty
Try
' Convert input values to numeric variables.
quantityInteger = Integer.Parse(.quantityTextBox.Text)
priceDecimal = Decimal.Parse(.priceTextBox.Text)
' Calculate values for sale.
extendedPriceDecimal = quantityInteger * priceDecimal
discountDecimal = extendedPriceDecimal * DISCOUNT_RATE_Decimal
discountedPriceDecimal = extendedPriceDecimal - discountDecimal
'Add to the discount total.
discountTotalDecimal += discountDecimal
' Save the discount total in a hidden field.
.discountTotalHiddenField.Value = discountTotalDecimal.ToString()
' Format and display answers.
.extendedPriceTextBox.Text = extendedPriceDecimal.ToString("C")
.discountTextBox.Text = discountDecimal.ToString("N")
.discountedPriceTextBox.Text = discountedPriceDecimal.ToString("C")
Catch ex As
Exception
.errorMessageLabel.Text = "Unable to
calculate. Check for numeric values."
End Try
End With
End Sub
Protected Sub
clearButton_Click(ByVal sender
As Object,
ByVal e As
System.EventArgs) Handles clearButton.Click
' Clear previous amounts from the page.
With Me
.quantityTextBox.Text = ""
.titleTextBox.Text = ""
.priceTextBox.Text = ""
.extendedPriceTextBox.Text = ""
.discountTextBox.Text = ""
.discountedPriceTextBox.Text = ""
.discountTotalLabel.Text = ""
.errorMessageLabel.Text = ""
End With
End Sub
Sub Page_Load(ByVal
sender As Object,
ByVal e As
System.EventArgs) Handles
Me.Load
' If a value exists for the discount total...
With Me.discountTotalHiddenField
If IsPostBack
And .Value <> ""
Then
discountTotalDecimal = Decimal.Parse(.Value)
End If
End With
End Sub
Protected Sub
summaryButton_Click(ByVal sender
As Object,
ByVal e As
System.EventArgs) Handles summaryButton.Click
' Display the total discount.
Me.discountTotalLabel.Text _
= "Total Discounts: $" &
Me.discountTotalHiddenField.Value
End Sub
End Class