IS360ASPLabx

rouleaupromiseΑσφάλεια

5 Νοε 2013 (πριν από 3 χρόνια και 1 μήνα)

48 εμφανίσεις

IS 360

ASP.NET Lab

EKEDAHL

In this
part of the
lab
, you will create an ASP.NET page
to

calculate the payment on a loan.

The input
values include the loan amount, interest rate, and loan term.

You can complete this assignment in
either VB or C#. My code
snippets appear in Visual Basic.

Note that you could also create this
application in JavaScript. This lab shows a different alternative.

1.

Start Visual Studio and create a new ASP.NET project.

To do this, click
File, New Web Site
. You
will see a dialog box s
imilar to the following: Set the project location as you see fit.
Name the
Web site as you see fit.
Y
ou
can
create the project in Visual Basic

of C#
.

Make sure that you have
selected and
ASP.NET
Empty
Web Site
.




Make sure that you create an
ASP.NET Empt
y Web Site
. If you create an
ASP.NET Web Si
te
,
several files will be created as part of a more complicated template. These files are beyond the
scope of this lab and are discussed in detail in IS 460.





Next, yo
u need to add an ASP.NET Web form to the pr
oject.


1.

Click
Web Site, Add New Item
to display the following dialog box:




2.

Make sure that
Web form

is selected.
By default,
the

Web Form

is created
having the name

Default.aspx
.

Click Add to add the Web form. Note that the Web form appears in the
Solution
Explorer as shown in the following figure.





The file Default.aspx.cs contains the C# code. (The File will be named Default.aspx.vb if you
are using Visual Basic.



The file Default.aspx contains the XHTML code where you create the user interface.



This file appears in the editor.

o

The
Source

tab displays the XHTML source code.

o

The
Design

tab displays a visual interface.

o

The
Split

tab is used to split the above windows.


If you are using C#, you must add a reference to the Visual Basic library because
, the PMT function
resides in that library.


1.

On the menu bar, click
Website
,
Add Reference

to display the Add Reference dialog
. Click the
.NET tab, if necessary.
Select
Microsoft.VisualBasic

as follows:




2.

Click
OK

to add the library reference.


3.

If you look at the web.config file, you will see that the Microsoft.VisualBasic assembly was added
to the project.




1.

Create the user interface for the application
as follows:

Use the
Standard

toolbox controls to
create the user interface.




I suggest viewing both the Design and Source
window
s
.



Make sure that you create all of the controls inside the region of the <form> tag. Remember
that an ASP.NET application can have only one <form> tag designated runat=”server.



Use text boxes for the
inpu
t values. Create descriptive labels to the left of the text boxes.




Looking at the following code, make sure that you correctly set the ID property. If you do
not, code that you will write later in the lab will not work correctly.



Here is some sample code
and a corresponding image:


<
body
>


<
form

id
="form1"

runat
="server">


<
div
>


<
asp
:
Label

ID
="Label1"

runat
="server"

Text
="Loan Amount"></
asp
:
Label
>


<
asp
:
TextBox

Width
="300px"

ID
="txtLoanAmount"


runat
="server"></
asp
:
TextBox
><
br

/>




<
asp
:
Label

ID
="Label2"

runat
="server"


Text
="Interest Rate (Annual)"></
asp
:
Label
>


<
asp
:
TextBox

width
="300px"

ID
="txtIR"

runat
="server"></
asp
:
TextBox
>

<
br

/>




<
asp
:
Label

ID
="Label3"

runat
="server"

Text
="Loan Term
(Years)"></
asp
:
Label
>


<
asp
:
TextBox

width
="300px"

ID
="txtTerm"

runat
="server"></
asp
:
TextBox
>

<
br

/>


</
div
>


</
form
>

</
body
>




Finally create a Label and corresponding prompt to display the output value (Loan
payment)
.


3.

Create a button on the form that will postback the page.

(Set the
PostBackUrl

property to
the name of the page, which is ~/Default.aspx). Set the
Text

property to Calculate Payment.
Use the Properties window to set these properties.
Again, make sure to use the ASP button
rather than the
HTML button.


4.

All of the coding will take place in the
button’s
Click

event handler. You use the same Code
Editor that you used to create the event handlers for desktop applications.
To create the eve
nt
handler, select the button in the designer, and press
F4

to display the Properties window. Select
the Events pane (the button that looks like a lightning bolt). Double
-
click the Click event.



5.

To activate the Code Editor, select the page (Default.aspx)

in the Solution Explorer. Then, click
View
Code

on the menu bar.

The following figures show the Code Editor for VB and C# respectively





6.

Next, create the code to calculate the payment of the loan.

Attached to the lab is a copy of the
Help page for th
e PMT function.

Note that the FV is 0 because the loan will be paid back at the
end of the term.

Here is some code to get you started. This code should appear inside of the
button’s
Click

event handler:



Dim

LoanAmount
As

Double


Dim

LoanTerm
As

Double


Dim

InterestRate
As

Double


Dim
Payment

As Double



LoanAmount =
System.Convert.ToDouble
(txtLoanAmount.Text)


LoanTerm =
System.Convert.ToDouble(txtTerm.Text)



InterestRate = System.Convert.ToDouble(txtIR.
Text)



' Calculate the monthly interest rate and loan term.



LoanTerm = LoanTerm * 12


InterestRate = InterestRate / 12



' Calculate the payment.


Payment = Microsoft.VisualBasic.
Financial
.Pmt(InterestRate,

_




LoanTerm,
LoanAmount, 0);




' Display the output
result.


txtOutput.Text = Payment.ToString();



The following code segment shows the same code written in C#:


Double

LoanAmount;


Double

LoanTerm;


Double

InterestRate;


Double

Payment;



LoanAmount = System.
Convert
.ToDouble(txtLoanAmount.Text);


LoanTerm = System.
Convert
.ToDouble(txtTerm.Text);


InterestRate = System.
Convert
.ToDouble(txtIR.Text);



LoanTerm = LoanTerm * 12;


InterestRate

= InterestRate / 12;




Payment = Microsoft.VisualBasic.
Financial
.Pmt(InterestRate, LoanTerm,


LoanAmount, 0);



txtOutput.Text = Payment.ToString();



7.

Test the program by pressing F5.
You might see the following window
:

Set the option as shown
in the following figure:




8.

You will see the application compile and then appear in a browser window as follows. Enter the
values shown in the following figure and click the button.
The results appear in the output
control.




9.

Close the browser window and end the program.


10.

Modify the program to deal with a loan that is not paid off at the end of the term. Here, the user
will need to enter a value for the FV argument. In addition, you will need to create another input
field.


In
the next part of the lab, you will create a second form that will utilize a table and a loop.


1.

Click
Website
,
Add New Item
. In the Add New Item dialog box, create a Web Form named
SalesTaxTable.aspx.


2.

Using the Toolbox’s Standard tab, create a Table on the

form. The code

should look like the
following. Make sure to set the ID property to tblTax.




3.

Activate the Code Editor for the form, and enter the following in the
Page’s Load event handler.
This event handler should already be created.



public

partial

class

SalesTaxTable

: System.Web.UI.
Page

{


protected

void

Page_Load(
object

sender,
EventArgs

e)


{


TableRow

tr =
new

TableRow
();


TableCell

tc =
new

TableCell
();


tc.Text =
"Property Value"
;


tr.Cells.Add(tc);


tc

=
new

TableCell
();


tc.Text =
"Tax Amount"
;


tr.Cells.Add(tc);


tblTax.Rows.Add(tr);



for

(
int

x = 100000; x <= 200000; x+=10000)


{


tr =
new

TableRow
();


tc =
new

TableCell
();


tc.Text

= x.ToString();


tr.Cells.Add(tc);


tc =
new

TableCell
();


tc.Text = (x * 0.015).ToString(
"f2"
);


tr.Cells.Add(tc);


tblTax.Rows.Add(tr);




}


}

}


4.

Run the program again. The tax

table should appear in the browser window.


5.

Enhance the program so that the above code will execute when the user clicks a button. You will
need to create this button. Allow the user to enter the minimum and maximum value. Use this
user input to control t
he repetition counters in the loop. Allow the user

to enter a specific tax
rate.