In the following example, a web service is what calculates the value of a
car based on its make, model, and how much it has depreciated in value. Depreciation is
not something that can normally be calculated on the client because it is based on a
complex formula that uses database lookups. For this example, the depreciation will simply
be calculated as $2,000 in value for each year the car has aged.
CHAPTER 3 ?– THE MICROSOFT AJAX LIBRARY: MAKING CLIENT-SIDE JAVASCRIPT EASIER 49
First you need to add a new web service item to your Visual Studio 2005 project and
name it CarService.asmx. Add a new WebMethod to the web service named getCarValue.
You??™ll need to add the following using statements at the top of the code file to provide
access to the ASP.NET 2.0 AJAX Extensions??™ attributes and keywords:
using System.Web.Script;
using System.Web.Script.Services;
Now here??™s the code for your getCarValue method:
[WebMethod]
public int getCarValue(string strCarMake,
string strCarModel,
int strCarYear)
{
int nReturn = 0;
if (strCarMake == "Honda")
{
if (strCarModel == "Pilot")
{
nReturn = 40000;
}
else
{
nReturn = 30000;
}
}
else
{
nReturn = 20000;
}
int nDepreciation = (System.
Pages:
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104