一起来学ASP.NET Ajax(五)之使用服务
2013-02-04 15:09
531 查看
在ASP.NET 中,除了上几篇博客中介绍的通信方式,还可以使用服务的方式进行通信,如下所示。
Employee类
Employee类
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// Employee类 /// </summary> public class Employee { private string _FirstName; private string _LastName; private string _Title; public Employee() { } /// <summary> /// 带参数构造函数 /// </summary> /// <param name="firstName">姓</param> /// <param name="lastName">名</param> /// <param name="title">职位</param> public Employee(string firstName, string lastName, string title) { this._FirstName = firstName; this._LastName = lastName; this._Title = title; } /// <summary> /// 姓 /// </summary> public string FirstName { get { return this._FirstName; } } /// <summary> /// 名 /// </summary> public string LastName { get { return this._LastName; } } /// <summary> /// 职位 /// </summary> public string Title { get { return this._Title; } } }客户端代码
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"></script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Web Service Access</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <%--注册WebService,指明使用的服务路径--%> <asp:ServiceReference Path="EmployeeServiece.asmx" /> </Services> </asp:ScriptManager> <script language="javascript" type="text/javascript"> //相当于服务器端的代理,将回调函数传入即可 function showEmployee(firstName, lastName, title) { //直接使用服务器端的方法 AspNetAjaxOverview.EmployeeService.GetEmployee( firstName, lastName, title, onGetEmployeeSuccess); } //回调函数,直接使用employee对象 function onGetEmployeeSuccess(employee) { alert(String.format( "Hello I'm {0} {1}, my title is '{2}'", employee.FirstName, employee.LastName, employee.Title)); } </script> <input type="button" value="Bill Gates" onclick="showEmployee('Bill', 'Gates', 'Chair man')" /> <input type="button" value="Steve Ballmer" onclick="showEmployee('Steve', 'Ballmer', 'CEO')" /> </form> </body> </html>WebService
<%@ WebService Language="C#" Class="AspNetAjaxOverview.EmployeeService" %> using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using System.Web.Script.Services; //这个是通过创建web服务添加的 namespace AspNetAjaxOverview { //命名空间 [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 //允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务(这里需要取消默认的注释) [System.Web.Script.Services.ScriptService] public class EmployeeService : System.Web.Services.WebService { [WebMethod] //添加下面标记后可以在客户端使用该方法 [ScriptMethod] //这里的方法是之前C#写的代码,与前台的Ajax语法无关,只是被调用而已 public Employee GetEmployee(string firstName, string lastName, string title) { return new Employee(firstName, lastName, title); } } }运行结果
相关文章推荐
- 在ASP.NET AJAX中使用应用程序服务和本地化(0):目录
- 在ASP.NET AJAX中使用应用程序服务和本地化:实现用户登录和注销(转自:Dflying Chen @ cnblogs)
- 在ASP.NET AJAX中使用应用程序服务和本地化(5):自定义应用程序服务的服务器端实现
- 在ASP.NET AJAX中使用应用程序服务和本地化(2):示例程序:实现用户登录和注销
- WCF服务在JavaScript中使用ASP.NET的AJAX方法
- 在ASP.NET AJAX中使用应用程序服务和本地化(6):创建支持多语言的ASP.NET AJAX站点、小结
- WCF服务在JavaScript中使用ASP.NET的AJAX方法
- 在 ASP.NET AJAX 中使用 Web 服务
- 使用ASP.NET AJAX 从脚本中调用Web 服务的应用方法
- 使用ASP.Net 3.5 的Ajax与Web服务开发实例
- 使用ASP.Net 3.5 的Ajax与Web服务开发实例
- 【转】 在 ASP.NET 3.5 中使用同时支持 Ajax Json 和 .NET 客户端的 WCF 服务
- 在ASP.NET AJAX中使用应用程序服务和本地化(3):用户个性化组件ProfileService
- 在ASP.NET AJAX中使用应用程序服务和本地化(4):示例程序:读取、修改并保存用户个性化信息
- 在ASP.NET AJAX中使用应用程序服务和本地化(5):自定义应用程序服务的服务器端实现
- 在ASP.NET AJAX中使用应用程序服务和本地化(1):身份认证组件AuthenticationService
- 关于AJAX跨域调用ASP.NET MVC或者WebAPI服务的问题及解决方案
- WebPart-----asp.net ajax CTP 拖拽控件使用时的数据库连接问题
- Asp.net Ajax 学习笔记5 UpdatePanel的使用(下)
- asp.net使用qq邮箱服务发邮件