Вы находитесь на странице: 1из 10

Richard Tham - ASP.

NET Training – Part 1

What Is ASP.NET?

Have you ever wondered how dynamic websites like Amazon.com work behind the
scenes? As a shopper at Amazon.com, you are shown a particular web page, but the web
page's content is dynamic, based on your preferences and actions. For example, if you
have an account with Amazon.com, when you visit Amazon.com's home page, your
name is shown at the top, and a list of personal recommendations is presented further
down the page. When you type a book's title into the search text box, a list of matching
books appears. When you click on a particular book's title, you are shown the book's
details along with comments from other users and an overall rating. When you add the
book to your shopping cart and check out, you are prompted for a credit card number,
which is then billed.

Web pages in websites whose content is determined dynamically based on user input or
other information are called dynamic web pages. For example, any website's search
engine page is an example of a dynamic web page because the content of the search page
is based on the search criteria the user entered and the documents on the web server.
Another example is Amazon.com's personal recommendations. The books and products
that Amazon.com suggests when you view the page are different from the books and
products suggested when someone else views these pages. Specifically, the
recommendations at Amazon.com are generated by products you have viewed as well as
previous purchases you have made.
Richard Tham - ASP.NET Training – Part 1

Creating A Financial Calculator Program:

1. 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.

2. 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.

3. 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.

4. 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.
Richard Tham - ASP.NET Training – Part 1

5. 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.

6. 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.

7. The 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.

8. 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.

9. 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].
Richard Tham - ASP.NET Training – Part 1

10. Add in this code to the source view for the button click event:

'Specify constant values


Const INTEREST_CALCS_PER_YEAR As Integer = 12
Const PAYMENTS_PER_YEAR As Integer = 12

'Create variables to hold the values entered by the user


Dim P As Double = loanAmount.Text
Dim r As Double = rate.Text / 100
Dim t As Double = mortgageLength.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.Exp(INTEREST_CALCS_PER_YEAR * Math.Log(1 +
ratePerPeriod)) - 1

Dim intPerPayment As Double


intPerPayment = (Math.Exp(Math.Log(annualRate + 1) /
payPeriods) - 1) * payPeriods

'Now, compute the total cost of the loan


Dim intPerMonth As Double = intPerPayment / PAYMENTS_PER_YEAR

Dim costPerMonth As Double


costPerMonth = P * intPerMonth / (1 - Math.Pow(intPerMonth +
1, -payPeriods))

'Now, display the results in the results Label Web control


results.Text = "Your mortgage payment per month is $" &
costPerMonth
Richard Tham - ASP.NET Training – Part 1

Declaring and Using Variables

A variable is a location in the computer's memory where you can temporarily store
information, such as a number or a string.

Declaring a Variable

Dim age as Integer


Dim variableName as type

Dim variableName as Integer


Dim variableName as Long
Dim variableName as Short
Dim variableName as Decimal
Dim variableName as Boolean
Dim variableName as Date

Dim a as Integer
a = 6

Dim a as Integer = 6

Arithmetic Operators

The four most-used arithmetic operators in Visual Basic are +, -, *, and /, which perform
addition, subtraction, multiplication, and division, respectively. These operators are
referred to as binary operators because they operate on two variables.

For example, to add together two integer variables and store the result in an integer
variable, you could use the following code:

Dim a, b, c as Integer
b = 15
c = 20
a=b+c

Visual Basic .NET's Comparison Operators


Operator Description
< Less than
<= Less than or equal
> Greater than
>= Greater than or equal
Richard Tham - ASP.NET Training – Part 1

Visual Basic .NET's Comparison Operators


Operator Description
= Equal
<> Not equal

The following statements evaluate to true:

4<8

3.14159 >= 2

"Bob" <> "Sue"

(10/2) = (20/4)

4 <= 4

The following statements evaluate to False:

7 > 100

"Bob" = "Frank"

(10/2) = 7.5

4<4

Understanding the Concatenation Operator

The concatenation operator concatenates two string variables. Concatenating two strings
produces a string that consists of the content of the first string with the content of the
second string appended. The string concatenation operator in Visual Basic is the
ampersand, &.

Dim firstWord as String = "ASP.NET"


Dim secondWord as String = "is"
Dim thirdWord as String = "neat"

Dim sentence as String


sentence = firstWord & " " & secondWord & " " & thirdWord & "."
Richard Tham - ASP.NET Training – Part 1

Visual Basic's Assignment Operators


variableName = value

For example, to assign the value 5 to an integer variable, we can use the following code:

Dim age as Integer


age = 5

'Create three integer variables


Dim sum, number1, number2 as Integer
number1 = 15
number2 = 20

'Assign the sum of number1 and number2 to sum


sum = number1 + number2

Adding value to the current value of variableName and then storing the resulting value
of this addition back into variableName. The following two lines have exactly the same
meaning and produce exactly the same resultsincrementing someIntegerVariable by 1:

someIntegerVariable = someIntegerVariable + 1

and

someIntegerVariable += 1

The Shorthand Assignment Operators


Operator Description
+= variable += value adds value to the value of variable and then stores this
resulting value back into variable.
-= variable -= value subtracts value from the value of variable and then
stores this resulting value back into variable.
*= variable *= value multiplies value to the value of variable and then
stores this resulting value back into variable.
/= variable /= value divides value into the value of variable and then stores
this resulting value back into variable. Recall that the / operator returns a
nonintegral value.
&= variable &= value concatenates value to the value of variable and then
stores this resulting value back into variable.
Richard Tham - ASP.NET Training – Part 1

Program Flow with Visual Basic's Control Structures


If condition Then
Instruction1
Instruction2
...
InstructionN
End If

[View full width]


1: Partial Class TimeAppropriateMessage
2: Inherits System.Web.UI.Page
3:
4: Protected Sub Page_Load(ByVal sender As Object, ByVal e
As System.EventArgs)
Handles Me.Load
5: If DateTime.Now.Hour >= 6 And DateTime.Now.Hour < 12
Then
6: lblMessage.Text = "Good morning."
7: End If
8:
9: If DateTime.Now.Hour >= 12 And DateTime.Now.Hour <=
17 Then
10: lblMessage.Text = "Good afternoon."
11: End If
12:
13: If DateTime.Now.Hour > 17 Or DateTime.Now.Hour < 6
Then
14: lblMessage.Text = "Good evening."
15: End If
16: End Sub
17: End Class

If password = "shazaam" then


'Display sensitive information
End If

If password <> "shazaam" then


'Display message informing the user they've entered an
'incorrect password
End If

If condition Then
Instruction1
Instruction2
...
InstructionN
Else
ElseInstruction1
ElseInstruction2
...
Richard Tham - ASP.NET Training – Part 1

ElseInstructionN
End If

If password = "shazaam" then


'Display sensitive information
Else
'Display message informing the user they've entered an
'incorrect password
End If

Working with Visual Basic's Looping Control Structures

Looping control structures allow for a set of instructions to be executed a repeated


number of times. The number of times the code is repeated can be a fixed number of
times, such as with For ... Next loops, or repeated until some condition is met, such as
with Do ... Loop loops.

For integerVariable = start to stop


Instruction1
Instruction2
...
InstructionN
Next integerVariable

Dim i as Integer
For i = 1 to 3
LabelWebControl.Text &= "Hello, World!"
Next i

Dim evens as Integer


For evens = 0 to 10 Step 2
LabelWebControl.Text &= evens & " is an even number.<br />"
Next evens

0 is an even number.
2 is an even number.
4 is an even number.
...
10 is an even number.

Do ... Loop Loops


Do While condition
Instruction1
Richard Tham - ASP.NET Training – Part 1

Instruction2
...
InstructionN
Loop
Dim number as Integer = 0
Do While number <= 10
LabelWebControl.Text &= number & " is an even number.<br />"
number += 2
Loop

Try These Exercises:

1. Write an ASP.NET script to enter two integers, obtains the numbers from the
user and outputs to a label that displays the larger number followed by the
words “is larger”. If the numbers are equal, output to a label that diplays the
message “These numbers are equal”

2. Write an ASP.NET script that takes three integers from the user and display
the sum, average, product smallest and largest of the numbers in a label.