ASP.NET AJAX调用Web Service

 


原文发布日期:2007.02.08
作者:Bipin Joshi
翻译:webabcd


介绍
尽管AJAX是种客户端技术,但实际上的开发过程,它经常要调用一个服务器端的过程。通常,网站上的数据是存放在一个关系型数据库中,为了让AJAX更有用处,处理服务器端数据需要一种简单可靠的方法。幸运的是,ASP.NET AJAX提供了一种有效的基础架构来做这件事情,浏览器和服务器在Internet上可以进行AJAX通信。自然而然,Web Service在数据传输和客户端/服务器之间的一般通信方面可以扮演一个重要角色。本文就演示了如果通过ASP.NET AJAX调用ASP.NET web services。


软件需求
本文所有的范例都是使用ASP.NET AJAX RC版,而且,要在SQL Server 2005 (Express版即可)上有一个Northwind数据库。范例使用Visual Studio 2005作为开发环境。


范例场景
范例开发了一个Web页面,用于输入Northwind数据库职员表中的职员数据。页面通过ASP.NET AJAX功能,调用一个Web Service来完成职员表中的数据增、删、改、查。


创建一个Web Service
作为开始,使用Visual Studio 2005创建一个新的Web站点,注意把ASP.NET AJAX项目模板添加到新站点对话框,这个对话框包括一个"ASP.NET AJAX Enabled Web Site" 模板。
ASP.NET AJAX调用Web Service
图1:新站点创建模板

使用"ASP.NET AJAX Enabled Web Site" 模板创建的新站点和用普通方法创建的站点区别如下:
    ·它的Web.config自动包括许多ASP.NET AJAX专用的配置信息。
    ·System.Web.Extensions程序集被添加到引用中。

当然,我们可以更改一个普通的Web站点,以使之符合AJAX要求,但模板可以大大简化我们的工作。

现在我们创建了一个新的Web站点,添加一个新的web service并命名为EmployeeService.asmx,EmployeeService将包括5个Web方法
Method Name Description
GetEmployees() 返回Employees表里的雇员列表。 这个列表是一个Employee对象数组
GetEmployee() 接收EmployeeID参数返回Employee对象的详细信息
Insert() 给Employees表里增加一个新的雇员信息
Update() 更新Employees表里的某个雇员信息
Delete() 删除Employees表里的某个雇员信息
表1:EmployeeService中的Web方法

GetEmployees() 和 GetEmployee()方法以Employee对象的形式返回数据,因此,首先创建一个Employee类。右键单击App_Code文件夹,选择“添加新项…”,添加一个叫Employee的类,下面显示Employee类的全部代码:
ASP.NET AJAX调用Web Servicepublic class Employee
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   
private int intEmployeeID;
ASP.NET AJAX调用Web Service   
private string strFirstName;
ASP.NET AJAX调用Web Service   
private string strLastName;
ASP.NET AJAX调用Web Service   
public int EmployeeID
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
get
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service      
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service         
return intEmployeeID;
ASP.NET AJAX调用Web Service      }

ASP.NET AJAX调用Web Service      
set
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service      
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service         intEmployeeID 
= value;
ASP.NET AJAX调用Web Service      }

ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service   
public string FirstName
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
get
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service      
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service         
return strFirstName;
ASP.NET AJAX调用Web Service      }

ASP.NET AJAX调用Web Service      
set
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service      
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service         strFirstName 
= value;
ASP.NET AJAX调用Web Service      }

ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service   
public string LastName
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
get
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service      
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service         
return strLastName;
ASP.NET AJAX调用Web Service      }

ASP.NET AJAX调用Web Service      
set
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service      
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service         strLastName 
= value;
ASP.NET AJAX调用Web Service      }

ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service}

Employee类申明三个Private变量来分别存放employee ID, first name和 last name,三个变量再封装在三个public属性中:EmployeeID, FirstName和LastName。

打开 web.config文件,添加<connectionStrings>部分如下:
ASP.NET AJAX调用Web Service<connectionStrings>
ASP.NET AJAX调用Web Service   
<add name="connstr" connectionString=
ASP.NET AJAX调用Web Service        "data source=.\sqlexpress;
ASP.NET AJAX调用Web Service        initial catalog=northwind;
ASP.NET AJAX调用Web Service        integrated security=true"
/>
ASP.NET AJAX调用Web Service
</connectionStrings>

这部分存放数据库链接字符串,用于指向Northwind数据库,确保修改SqlServer名称、IP地址以及验证方式以和我们的开发环境相符。

现在,打开EmployeeService.cs添加如下代码:
ASP.NET AJAX调用Web Serviceprivate string strConn =   "";
ASP.NET AJAX调用Web Service
public EmployeeService()
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   strConn 
= ConfigurationManager.ConnectionStrings["connstr"].
ASP.NET AJAX调用Web Service             ConnectionString;
ASP.NET AJAX调用Web Service}

代码使用了ConfigurationManager类来读取配置文件中的数据库链接字符串,并存放在一个类级别的变量strConn中,这个变量将被下面的所有Web Method所使用。

现在,添加GetEmployees() web method:
ASP.NET AJAX调用Web Service[WebMethod]
ASP.NET AJAX调用Web Service
public Employee[] GetEmployees()
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   SqlConnection cnn 
= new SqlConnection(strConn);
ASP.NET AJAX调用Web Service   cnn.Open();
ASP.NET AJAX调用Web Service   SqlCommand cmd            
= new SqlCommand();
ASP.NET AJAX调用Web Service   cmd.Connection            
= cnn;
ASP.NET AJAX调用Web Service   cmd.CommandText           
= "select employeeid,firstname,
ASP.NET AJAX调用Web Service
                                lastname from employees";
ASP.NET AJAX调用Web Service
   SqlDataReader reader      = cmd.ExecuteReader();
ASP.NET AJAX调用Web Service   List
<Employee> list = new List<Employee>();
ASP.NET AJAX调用Web Service   
while (reader.Read())
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      Employee emp   
= new Employee();
ASP.NET AJAX调用Web Service      emp.EmployeeID 
= reader.GetInt32(0);
ASP.NET AJAX调用Web Service      emp.FirstName  
= reader.GetString(1);
ASP.NET AJAX调用Web Service      emp.LastName   
= reader.GetString(2);
ASP.NET AJAX调用Web Service      list.Add(emp);
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service   reader.Close();
ASP.NET AJAX调用Web Service   cnn.Close();
ASP.NET AJAX调用Web Service   
return list.ToArray();
ASP.NET AJAX调用Web Service}

代码创建了SqlConnection and SqlCommand 对象,然后执行SELECT查询,以获取Employees表中EmployeeID, FirstName 和LastName字段。结果通过SqlDataReader返回。然后,创建一个generic-based Employee数组,通过While循环,给每个Employee实例的属性赋值。当While循环完毕的时候,关闭SqlDataReader 和 SqlConnection。GetEmployees()方法返回的类型是Employee数组。因此,generic List使用List类中的ToArray()方法来转换成Employee数组。

现在,添加一个GetEmployee() web method如下:
ASP.NET AJAX调用Web Service[WebMethod]
ASP.NET AJAX调用Web Service
public Employee GetEmployee(int pEmployeeId)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   SqlConnection cnn 
= new SqlConnection(strConn);
ASP.NET AJAX调用Web Service   cnn.Open();
ASP.NET AJAX调用Web Service   SqlCommand cmd       
= new SqlCommand();
ASP.NET AJAX调用Web Service   cmd.Connection       
= cnn;
ASP.NET AJAX调用Web Service   cmd.CommandText      
= "select employeeid,firstname,lastname
ASP.NET AJAX调用Web Service
                           from employees where employeeid=@id";
ASP.NET AJAX调用Web Service
   SqlParameter id      = new SqlParameter("@id", pEmployeeId);
ASP.NET AJAX调用Web Service   cmd.Parameters.Add(id);
ASP.NET AJAX调用Web Service   SqlDataReader reader 
= cmd.ExecuteReader();
ASP.NET AJAX调用Web Service   Employee emp         
= new Employee();
ASP.NET AJAX调用Web Service   
while (reader.Read())
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      emp.EmployeeID 
= reader.GetInt32(0);
ASP.NET AJAX调用Web Service      emp.FirstName  
= reader.GetString(1);
ASP.NET AJAX调用Web Service      emp.LastName   
= reader.GetString(2);
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service   reader.Close();
ASP.NET AJAX调用Web Service   cnn.Close();
ASP.NET AJAX调用Web Service   
return emp;
ASP.NET AJAX调用Web Service}

GetEmployee() web method接受一个employee ID参数作为输入,代码和前面的非常相似,但这次只返回一个employee。注意,使用SqlParameter来定义传入的EmployeeID。

现在,再添加Insert()、Update()和 Delete()web methods,其中,Insert() web method 以要添加的Employee的 first name 和 last name 作为参数,Update() web method 以要更新的employee ID 以及新的first name 和 last name作为参数,并执行UPDATE语句, Delete() web method 以要删除的employee ID 作为参数,然后执行DELETE 语句
ASP.NET AJAX调用Web Service[WebMethod]
ASP.NET AJAX调用Web Service
public int Insert(string pFirstName, string pLastName)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   SqlConnection cnn  
= new SqlConnection(strConn);
ASP.NET AJAX调用Web Service   cnn.Open();
ASP.NET AJAX调用Web Service   SqlCommand cmd     
= new SqlCommand();
ASP.NET AJAX调用Web Service   cmd.Connection     
= cnn;
ASP.NET AJAX调用Web Service   cmd.CommandText    
= "insert into employees(firstname,lastname)
ASP.NET AJAX调用Web Service
                         values (@fname,@lname)";
ASP.NET AJAX调用Web Service
   SqlParameter fname = new SqlParameter("@fname", pFirstName);
ASP.NET AJAX调用Web Service   SqlParameter lname 
= new SqlParameter("@lname", pLastName);
ASP.NET AJAX调用Web Service   cmd.Parameters.Add(fname);
ASP.NET AJAX调用Web Service   cmd.Parameters.Add(lname);
ASP.NET AJAX调用Web Service   
int i = cmd.ExecuteNonQuery();
ASP.NET AJAX调用Web Service   cnn.Close();
ASP.NET AJAX调用Web Service   
return i;
ASP.NET AJAX调用Web Service}

ASP.NET AJAX调用Web Service[WebMethod]
ASP.NET AJAX调用Web Service
public int Update(int pEmployeeId,string pFirstName, string pLastName)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   SqlConnection cnn  
= new SqlConnection(strConn);
ASP.NET AJAX调用Web Service   cnn.Open();
ASP.NET AJAX调用Web Service   SqlCommand cmd     
= new SqlCommand();
ASP.NET AJAX调用Web Service   cmd.Connection     
= cnn;
ASP.NET AJAX调用Web Service   cmd.CommandText    
= "update employees set [email protected],
ASP.NET AJAX调用Web Service
                         lastname=@lname where employeeid=@id";
ASP.NET AJAX调用Web Service
   SqlParameter fname = new SqlParameter("@fname", pFirstName);
ASP.NET AJAX调用Web Service   SqlParameter lname 
= new SqlParameter("@lname", pLastName);
ASP.NET AJAX调用Web Service   SqlParameter id 
= new SqlParameter("@id", pEmployeeId);
ASP.NET AJAX调用Web Service   cmd.Parameters.Add(fname);
ASP.NET AJAX调用Web Service   cmd.Parameters.Add(lname);
ASP.NET AJAX调用Web Service   cmd.Parameters.Add(id);
ASP.NET AJAX调用Web Service   
int i = cmd.ExecuteNonQuery();
ASP.NET AJAX调用Web Service   cnn.Close();
ASP.NET AJAX调用Web Service   
return i;
ASP.NET AJAX调用Web Service}

ASP.NET AJAX调用Web Service[WebMethod]
ASP.NET AJAX调用Web Service
public int Delete(int pEmployeeId)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   SqlConnection cnn 
= new SqlConnection(strConn);
ASP.NET AJAX调用Web Service   cnn.Open();
ASP.NET AJAX调用Web Service   SqlCommand cmd  
= new SqlCommand();
ASP.NET AJAX调用Web Service   cmd.Connection  
= cnn;
ASP.NET AJAX调用Web Service   cmd.CommandText 
= "delete from employees where [email protected]";
ASP.NET AJAX调用Web Service   SqlParameter id 
= new SqlParameter("@id", pEmployeeId);
ASP.NET AJAX调用Web Service   cmd.Parameters.Add(id);
ASP.NET AJAX调用Web Service   
int i = cmd.ExecuteNonQuery();
ASP.NET AJAX调用Web Service   cnn.Close();
ASP.NET AJAX调用Web Service   
return i;
ASP.NET AJAX调用Web Service}

这就完成了web service的创建。到目前为止,还没有做任何和AJAX特性相关的任何工作,现在,时机已经成熟,我们通过下面的代码更改web service类的定义:
ASP.NET AJAX调用Web Serviceusing System.Web.Script.Services;
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service[WebService(Namespace 
= "http://tempuri.org/")]
ASP.NET AJAX调用Web Service[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
ASP.NET AJAX调用Web Service[ScriptService]
ASP.NET AJAX调用Web Service
public class EmployeeService : System.Web.Services.WebService
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   ASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service   ASP.NET AJAX调用Web Service

注意特地标明的黑体字,我们导入了System.Web.Script.Services命名空间,这个命名空间来自System.Web.Extensions程序集,这个命名空间提供了[ScriptService]属性,这将使web service可以被来自客户端的JavaScript (如ASP.NET AJAX)调用。

好了,我们开始准备从ASP.NET AJAX调用Web Service了!


如何调用Web Service
这部分,我们将创建一个Web页面作为数据输入,通过调用刚刚创建的Web Service来操作Employees表。作为开始,我们先添加一个EmployeeServiceClient.aspx页面,打开工具箱,选择View > Toolbox菜单,在工具箱上,选中AJAX Extensions这样的节点(见图2)
ASP.NET AJAX调用Web Service
图 2: 增加模板后的新站点创建对话框

AJAX Extensions部分显示一个Web页面上所有可以使用的ASP.NET AJAX组件。所有使用ASP.NET AJAX的页面都需要一个ScriptManager组件。打开ScriptManager属性窗口,定位Services属性,打开Service引用编辑器,如图3:
ASP.NET AJAX调用Web Service
图 3: Service 引用编辑器

点击对话框底部的Add按钮,设置Path属性以指向Web Service(EmployeeService.asmx)的虚拟路径,下面的标记将会产生在Web页面文件中:
ASP.NET AJAX调用Web Service<asp:ScriptManager ID="ScriptManager1" runat="server" >
ASP.NET AJAX调用Web Service   
<Services>
ASP.NET AJAX调用Web Service      
<asp:ServiceReference Path="EmployeeService.asmx" />
ASP.NET AJAX调用Web Service   
</Services>
ASP.NET AJAX调用Web Service
</asp:ScriptManager>

对每个Web Service调用,都需要在<asp:ScriptManager>部分添加一个<asp:ServiceReference>元素,此标记把要使用的web service注册到当前web form上。
ASP.NET AJAX调用Web Service
图 4: 设计页面表单

表单包括一个下拉框(<SELECT>) ,用于显示所有的employee IDs,一旦选中其中一个employee ID,employee的详细信息将显示在2个文本框中,然后可以更新这些信息。如果要添加一个employee,只需要输入first name 和 last name,然后点击“插入”按钮就可以了。同理,如果要删除一个employee,选择下拉框中的employee ID,点击“删除”按钮。在INSERT、UPDATE或者 DELETE操作完成后,将会显示成功或者失败的信息。下面是所有的页面代码:
ASP.NET AJAX调用Web Service<table>
ASP.NET AJAX调用Web Service   
<tr>
ASP.NET AJAX调用Web Service      
<td colspan="2">
ASP.NET AJAX调用Web Service         
<asp:Label ID="Label4" runat="server" Font-Size="X-Large"
ASP.NET AJAX调用Web Service                    Text
="Employee Management">
ASP.NET AJAX调用Web Service         
</asp:Label></td>
ASP.NET AJAX调用Web Service   
</tr>
ASP.NET AJAX调用Web Service   
<tr>
ASP.NET AJAX调用Web Service      
<td style="width: 100px">
ASP.NET AJAX调用Web Service         
<asp:Label ID="Label1" runat="server"
ASP.NET AJAX调用Web Service                    Text
="Employee ID :"></asp:Label></td>
ASP.NET AJAX调用Web Service      
<td style="width: 100px">
ASP.NET AJAX调用Web Service         
<select id="Select1" >
ASP.NET AJAX调用Web Service         
</select>
ASP.NET AJAX调用Web Service      
</td>
ASP.NET AJAX调用Web Service   
</tr>
ASP.NET AJAX调用Web Service   
<tr>
ASP.NET AJAX调用Web Service      
<td style="width: 100px">
ASP.NET AJAX调用Web Service         
<asp:Label ID="Label2" runat="server"
ASP.NET AJAX调用Web Service                    Text
="First Name :"></asp:Label></td>
ASP.NET AJAX调用Web Service      
<td style="width: 100px">
ASP.NET AJAX调用Web Service         
<input id="Text1" type="text" /></td>
ASP.NET AJAX调用Web Service   
</tr>
ASP.NET AJAX调用Web Service   
<tr>
ASP.NET AJAX调用Web Service      
<td style="width: 100px">
ASP.NET AJAX调用Web Service         
<asp:Label ID="Label3" runat="server"
ASP.NET AJAX调用Web Service                    Text
="Last Name :"></asp:Label></td>
ASP.NET AJAX调用Web Service      
<td style="width: 100px">
ASP.NET AJAX调用Web Service         
<input id="Text2" type="text" /></td>
ASP.NET AJAX调用Web Service   
</tr>
ASP.NET AJAX调用Web Service   
<tr>
ASP.NET AJAX调用Web Service      
<td align="center" colspan="2">
ASP.NET AJAX调用Web Service         
<input id="Button3" type="button" value="Insert" />
ASP.NET AJAX调用Web Service         
<input id="Button4" type="button" value="Update" />
ASP.NET AJAX调用Web Service         
<input id="Button5" type="button" value="Delete" />
ASP.NET AJAX调用Web Service      
</td>
ASP.NET AJAX调用Web Service   
</tr>
ASP.NET AJAX调用Web Service   
<tr>
ASP.NET AJAX调用Web Service      
<td align="center" colspan="2">
ASP.NET AJAX调用Web Service         
<span id="lblMsg" style="font-weight: bold;
ASP.NET AJAX调用Web Service               color: red;"
></span>
ASP.NET AJAX调用Web Service      
</td>
ASP.NET AJAX调用Web Service   
</tr>
ASP.NET AJAX调用Web Service
</table>

注意:我们没有使用ASP.NET服务器端控件,如DropDownList、 TextBox 以及 Button。取而代之的是,我们用的传统的HTML控件,如:<SELECT> 以及 <INPUT>。这因为我们要想通过客户端JavaScript调用web service,而不是通过服务端代码。同理,注意底下的<SPAN>标记,这是用来显示成功或者失败的信息的。

下一步,在<head>元素内增加一个<script>部分,添加一个CallWebMethod()的函数:
ASP.NET AJAX调用Web Servicefunction CallWebMethod(methodType)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   
switch(methodType)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
case "select":
ASP.NET AJAX调用Web Service         EmployeeService.GetEmployees(FillEmployeeList,ErrorHandler,
ASP.NET AJAX调用Web Service                                      TimeOutHandler);
ASP.NET AJAX调用Web Service         
break;
ASP.NET AJAX调用Web Service      
case "selectone":
ASP.NET AJAX调用Web Service         var select
=document.getElementById("Select1");
ASP.NET AJAX调用Web Service         var empid
=select.options[select.selectedIndex].value;
ASP.NET AJAX调用Web Service         EmployeeService.GetEmployee(empid,DisplayEmployeeDetails,
ASP.NET AJAX调用Web Service                                     ErrorHandler,TimeOutHandler);
ASP.NET AJAX调用Web Service         
break;
ASP.NET AJAX调用Web Service      
case "insert":
ASP.NET AJAX调用Web Service         var text1
=document.getElementById("Text1");
ASP.NET AJAX调用Web Service         var text2
=document.getElementById("Text2");
ASP.NET AJAX调用Web Service         EmployeeService.Insert(text1.value,text2.value,
ASP.NET AJAX调用Web Service                                InsertEmployee,ErrorHandler,
ASP.NET AJAX调用Web Service                                TimeOutHandler);
ASP.NET AJAX调用Web Service         
break;
ASP.NET AJAX调用Web Service      
case "update":
ASP.NET AJAX调用Web Service         var select
=document.getElementById("Select1");
ASP.NET AJAX调用Web Service         var empid
=select.options[select.selectedIndex].value;
ASP.NET AJAX调用Web Service         var text1
=document.getElementById("Text1");
ASP.NET AJAX调用Web Service         var text2
=document.getElementById("Text2");
ASP.NET AJAX调用Web Service         var emp
=new Employee();
ASP.NET AJAX调用Web Service         emp.EmployeeID
=empid;
ASP.NET AJAX调用Web Service         emp.FirstName
=text1.value;
ASP.NET AJAX调用Web Service         emp.LastName
=text2.value;
ASP.NET AJAX调用Web Service         EmployeeService.Update(empid,text1.value,text2.value,
ASP.NET AJAX调用Web Service                                UpdateEmployee,ErrorHandler,
ASP.NET AJAX调用Web Service                                TimeOutHandler);
ASP.NET AJAX调用Web Service         
break;
ASP.NET AJAX调用Web Service      
case "delete":
ASP.NET AJAX调用Web Service         var select
=document.getElementById("Select1");
ASP.NET AJAX调用Web Service         var empid
=select.options[select.selectedIndex].value;
ASP.NET AJAX调用Web Service         EmployeeService.Delete(empid,DeleteEmployee,ErrorHandler,
ASP.NET AJAX调用Web Service                                TimeOutHandler);
ASP.NET AJAX调用Web Service         
break;
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service}

CallWebMethod() 函数 就是用来调用web service的*控制函数。 这个函数接收一个字符串参数用来标识调用的方法,它包括一个switch 语句来判断调用的方法,每个 case 块 调用一个web method。 注意web method 是如何被调用的:ASP.NET AJAX 框架自动创建一个JavaScript 代理类,这个代理类和要调用的web service有相同的名称。因此,上面代码中 EmployeeService 并不是真正的 类,而是一个JavaScript 代理类。 代理类包括原始web service中所有的Web Method。除了原来的web method 所包括的参数外,每个方法还包括3个额外的参数。

第一个参数是一个JavaScript 函数,用于当web method 成功完成时调用的。记住:所有客户端和服务器端的AJAX 通信都是异步的,因此,这个函数用来捕获web method 的返回值。第二个参数是用于发生错误的情况下调用的JavaScript 函数。最后,第三个参数是当调用Web Service 发生超时的情况下调用的JavaScript 函数。

第一种情况,case ("select"),就是简单的调用GetEmployees() 方法;第二种情况,case ("selectone"),调用GetEmployee()方法,通过传统的JavaScript 代码获取下拉框中的employee ID;同理,第三、第四、第五个Case依次调用Insert()、 Update()和 Delete()方法。

上面的代码通过5个JavaScript函数实现相应的web method 成功调用:FillEmployeeList(), DisplayEmployeeDetails(), InsertEmployee(), UpdateEmployee()以及 DeleteEmployee()。每个函数接收一个参数作为web method相应的返回值。
ASP.NET AJAX调用Web Servicefunction FillEmployeeList(result)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   
var select=document.getElementById("Select1");
ASP.NET AJAX调用Web Service   
for(var i=0;i<result.length;i++)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
var option=new Option(result[i].EmployeeID,
ASP.NET AJAX调用Web Service                            result[i].EmployeeID);
ASP.NET AJAX调用Web Service      select.options.add(option);
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service}

ASP.NET AJAX调用Web Service
function DisplayEmployeeDetails(result)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   
var text1=document.getElementById("Text1");
ASP.NET AJAX调用Web Service   
var text2=document.getElementById("Text2");
ASP.NET AJAX调用Web Service   text1.innerText
=result.FirstName;
ASP.NET AJAX调用Web Service   text2.innerText
=result.LastName;
ASP.NET AJAX调用Web Service   
var lblMsg=document.getElementById("lblMsg");
ASP.NET AJAX调用Web Service   lblMsg.innerText
="";
ASP.NET AJAX调用Web Service}

ASP.NET AJAX调用Web Service
function InsertEmployee(result)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   
if(result>0)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
var lblMsg=document.getElementById("lblMsg");
ASP.NET AJAX调用Web Service      lblMsg.innerText
="Employee added successfully!";
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service   
else
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
var lblMsg=document.getElementById("lblMsg");
ASP.NET AJAX调用Web Service      lblMsg.innerText
="Error occurred while adding new employee!";
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service}

ASP.NET AJAX调用Web Service
function UpdateEmployee(result)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   
if(result>0)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
var lblMsg=document.getElementById("lblMsg");
ASP.NET AJAX调用Web Service      lblMsg.innerText
="Employee updated successfully!";
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service   
else
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
var lblMsg=document.getElementById("lblMsg");
ASP.NET AJAX调用Web Service      lblMsg.innerText
="Error occurred while updating the employee!";
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service}

ASP.NET AJAX调用Web Service
function DeleteEmployee(result)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   
if(result>0)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
var lblMsg=document.getElementById("lblMsg");
ASP.NET AJAX调用Web Service      lblMsg.innerText
="Employee deleted successfully!";
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service   
else
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service   
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service      
var lblMsg=document.getElementById("lblMsg");
ASP.NET AJAX调用Web Service      lblMsg.innerText
="Error occurred while deleting employee!";
ASP.NET AJAX调用Web Service   }

ASP.NET AJAX调用Web Service}

FillEmployeeList() 函数以Employee对象数组作为输入参数,还记得GetEmployees() web method 返回的Employee对象数组吧。然后对这个数组迭代处理,在每次迭代中,一个新的OPTION元素被创建并添加到下拉框中。DisplayEmployeeDetails()函数以一个Employee对象作为输入,这个Employee对象包括了一个Employee的详细信息,并显示在2个文本框中。InsertEmployee(), UpdateEmployee()和DeleteEmployee()函数以一个整型的数值标明INSERT, UPDATE和DELETE操作所影响的记录数,一个大于0的数值标明操作成功,并在<SPAN>标记内显示一个成功的信息;否则,显示一个错误信息。当页面第一次加载时,需要用现有的employee ID给下拉框赋值,这得通过调用一个特定的名为pageLoad()的函数中实现:
ASP.NET AJAX调用Web Servicefunction pageLoad()
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   CallWebMethod(
"select");
ASP.NET AJAX调用Web Service}

ASP.NET AJAX调用Web Service

pageLoad()函数在客户端浏览器页面加载时自动调用,最后,错误处理(error handler)和超时处理(timeout handler)函数如下:
ASP.NET AJAX调用Web Servicefunction ErrorHandler(result)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   
var msg=result.get_exceptionType() + "\r\n";
ASP.NET AJAX调用Web Service   msg 
+= result.get_message() + "\r\n";
ASP.NET AJAX调用Web Service   msg 
+= result.get_stackTrace();
ASP.NET AJAX调用Web Service   alert(msg);
ASP.NET AJAX调用Web Service}

ASP.NET AJAX调用Web Service
function TimeOutHandler(result)
ASP.NET AJAX调用Web ServiceASP.NET AJAX调用Web Service
ASP.NET AJAX调用Web Service{
ASP.NET AJAX调用Web Service   alert(
"Timeout :" + result);
ASP.NET AJAX调用Web Service}

TimeOutHandler() 函数在任何web method 调用发生超时的情况下调用。它仅仅显示了一个Alert给用户。 ErrorHandler() 函数在有错误发生的情况下调用,其输入result 参数提供了3个方法:get_exceptionType()、get_message()以及 get_stackTrace()。这三个方法分别返回异常类型(type of exception)、详细错误信息 和堆栈跟踪(stack trace)。这里ErrorHandler()函数也仅仅显示了一个alert给终端用户。


测试网页
现在,我们已经实现了web service和客户端应用程序。测试一下吧!运行网页,试着增加、更新、删除一个employee看看,图5显示更新一个employee后的效果:
ASP.NET AJAX调用Web Service
图 5: 更新Employee后的页面效果

要想测试错误处理函数,把初始化数据库链接字符串改成一个空值,然后运行网页看看,这次,就会显示一个警报(alert),如图6:
ASP.NET AJAX调用Web Service
图 6: 链接字符串错误报警


调用外部Web Services
这个例子中,EmployeeService也是Web站点的一部分。有时候,我们的程序也许需要调用根本就没有部署在我们的域的web services。 ASP.NET AJAX内部需要依赖XML HTTP 对象,而由于安全原因,是不能和部署在其它外部站点进行通信的。这就意味着上面所说的技术对外部的web services调用无效。不幸的是,ASP.NET AJAX关于此问题还没有直接的解决方案(至少在RC版本)。然而,微软发布了一个仍在CTP阶段的“Bridge”技术,我们可以使用此技术来调用一个部署在本地的封装(Wrapper)的类,然后在这个类中来调用外部的实际的Web Service。在当前的RC版本中,我们可以在我们的Web Site中创建一个Wrapper Web Service,以它来调用最初的Web Service。然后在客户端程序中通过调用Wrapper Web Service实现通信。下面显示必要的步骤:
1. 在web站点中添加一个web引用,指向外部的Web service;
2. 创建一个本地Web service;
3. 在新创建的Web service中,提供封装的web method,这些方法调用外部的Web Method;
4. 用本文中所说的方法在客户端应用程序中调用本地新添加的web Service。


调用ASP.NET Web Services的基础架构
ASP.NET AJAX提供了完整的架构以从客户端JavaScript调用ASP.NET web services。我们可以轻松地用AJAX把服务器端数据集成进用户响应的Web页面中。而我们所需要做的就是仅仅用[ScriptService]属性来标识web Service。ASP.NET AJAX 框架会为我们的web service自动生成JavaScript代理,然后通过使用代理来调用web methods。


下载源码
[原文源码下载]


作者:Bipin Joshi
Email:http://www.dotnetbips.com/contact.aspx
简介:Bipin Joshi是DotNetBips.com的管理员。他是http://www.binaryintellect.com/的发起人,这个公司提供.NET framwork的培训和咨询服务。他在印度孟买为开发者提供培训。他也是微软的MVP(ASP.Net)和ASPInsiders的会员。


译者注:原文中的document.getElementById可以用$get来代替

转载于:https://www.cnblogs.com/jacklaw/archive/2007/07/02/802987.html