Asp.net 2.0 Ajax调用Web Service
2008-12-30 12:54
381 查看
一、准备工作
首先,建立一个ASP.NET AJAX CTP-Enalbed Web Site
然后,我们需要三个html控件,在aspx加入代码如下:
<input id="tbName" type="text" />
<input id="btnSubmit" type="button" value="button" /><br />
<label id="lblMessage"></label>
二、编写提供服务的WebService
在项目中建立一个Web Service,我们叫它HelloWorld.asmx,为其添加System.Web.Script.Services引用、ScriptService特性和一个Web Method GetHelloWorldCallBack。
整个cs代码如下:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services; //注意添加
/// <summary>
/// HelloWorld 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.19870123.cn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class HelloWorld : System.Web.Services.WebService {
public HelloWorld () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string GetHelloWorldCallBack(string userName) {
return string.Format("Hello World {0} , The Server Time is {1}", userName, DateTime.Now);
}
}
引用System.Web.Script.Services是为了让程序从客户端访问该Service所必需的。该WebMethod是接受一个字符参数,然后通过简单处理后返回。
三、使用ScriptManager控件对Web Service引用
我们回到aspx页面上,添加一个ScriptManager控件,并对其引用相关的webservice文件。
选中ScriptManager控件,在"属性"窗口中的Services属性添加一个Service,指定其Path为"~/HelloWorld.asmx"。确定后,HelloWorld.asmx就被引用到了ScriptManager中。由于程序中还有一些定义在Microsoft.Web.Preview.PreviewScript.js中的脚本要用到,我们还要添加对它的引用。
同样,对ScriptManager控件设置属性,这次为Scripts属性。我们添加一个Script,指定其Assembly为Microsoft.Web.Preview,Name为PreviewScript.js,这样页面中就会Include该js了。现在,该ScriptManager控件的代码如下:
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Services>
<asp:ServiceReference Path="~/HelloWorld.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Assembly="Microsoft.Web.Preview" Name="PreviewScript.js" />
</Scripts>
</asp:ScriptManager>
三、让程序运行起来!
做好了以上工作,我们将进入激动人心的时刻:让程序运行起来!
要让程序运行起来,我们还得借助JavaScript!我们先定义三个变量:
var tbName,btnSubmit,lblMessage;
然后,我们需要相关的方法,让程序运行起来。我们继承C#语法,叫它pageLoad方法,代码如下:
function pageLoad()
{
tbName=new Sys.Preview.UI.TextBox($get('tbName')); //建立TextBox
tbName.initialize(); //完成初始化
btnSubmit = new Sys.Preview.UI.Button($get('btnSubmit'));
btnSubmit.add_click(btnSubmit_Click); //添加事件
btnSubmit.initialize();
lblMessage = new Sys.Preview.UI.Label($get('lblMessage'));
lblMessage.initialize();
}
function btnSubmit_Click() //btnSubmit的Click事件
{
HelloWorld.GetHelloWorldCallBack(tbName.get_text(),callBack_helloWorld); //调用WebService的HelloWorld方法
}
function callBack_helloWorld(result)
{
lblMessage.set_text(result); //显示回调结果
}
首先,建立一个ASP.NET AJAX CTP-Enalbed Web Site
然后,我们需要三个html控件,在aspx加入代码如下:
<input id="tbName" type="text" />
<input id="btnSubmit" type="button" value="button" /><br />
<label id="lblMessage"></label>
二、编写提供服务的WebService
在项目中建立一个Web Service,我们叫它HelloWorld.asmx,为其添加System.Web.Script.Services引用、ScriptService特性和一个Web Method GetHelloWorldCallBack。
整个cs代码如下:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services; //注意添加
/// <summary>
/// HelloWorld 的摘要说明
/// </summary>
[WebService(Namespace = "http://www.19870123.cn/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class HelloWorld : System.Web.Services.WebService {
public HelloWorld () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string GetHelloWorldCallBack(string userName) {
return string.Format("Hello World {0} , The Server Time is {1}", userName, DateTime.Now);
}
}
引用System.Web.Script.Services是为了让程序从客户端访问该Service所必需的。该WebMethod是接受一个字符参数,然后通过简单处理后返回。
三、使用ScriptManager控件对Web Service引用
我们回到aspx页面上,添加一个ScriptManager控件,并对其引用相关的webservice文件。
选中ScriptManager控件,在"属性"窗口中的Services属性添加一个Service,指定其Path为"~/HelloWorld.asmx"。确定后,HelloWorld.asmx就被引用到了ScriptManager中。由于程序中还有一些定义在Microsoft.Web.Preview.PreviewScript.js中的脚本要用到,我们还要添加对它的引用。
同样,对ScriptManager控件设置属性,这次为Scripts属性。我们添加一个Script,指定其Assembly为Microsoft.Web.Preview,Name为PreviewScript.js,这样页面中就会Include该js了。现在,该ScriptManager控件的代码如下:
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Services>
<asp:ServiceReference Path="~/HelloWorld.asmx" />
</Services>
<Scripts>
<asp:ScriptReference Assembly="Microsoft.Web.Preview" Name="PreviewScript.js" />
</Scripts>
</asp:ScriptManager>
三、让程序运行起来!
做好了以上工作,我们将进入激动人心的时刻:让程序运行起来!
要让程序运行起来,我们还得借助JavaScript!我们先定义三个变量:
var tbName,btnSubmit,lblMessage;
然后,我们需要相关的方法,让程序运行起来。我们继承C#语法,叫它pageLoad方法,代码如下:
function pageLoad()
{
tbName=new Sys.Preview.UI.TextBox($get('tbName')); //建立TextBox
tbName.initialize(); //完成初始化
btnSubmit = new Sys.Preview.UI.Button($get('btnSubmit'));
btnSubmit.add_click(btnSubmit_Click); //添加事件
btnSubmit.initialize();
lblMessage = new Sys.Preview.UI.Label($get('lblMessage'));
lblMessage.initialize();
}
function btnSubmit_Click() //btnSubmit的Click事件
{
HelloWorld.GetHelloWorldCallBack(tbName.get_text(),callBack_helloWorld); //调用WebService的HelloWorld方法
}
function callBack_helloWorld(result)
{
lblMessage.set_text(result); //显示回调结果
}
相关文章推荐
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(6):服务器端和客户端数据类型的自动转换:复杂类型
- ASP.NET AJAX调用Web Service
- ASP.NET 2.0 AJAX中Webservice调用方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(6):服务器端和客户端数据类型的自动转换:复杂类型
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(1):调用Web Service、调用页面中的类方法
- ASP.NET AJAX 中在客户端用 WebRequest 调用 Web Service
- asp.net ajax程序设计之笔记1-- Hello World调用web service
- ASP.NET AJAX1.0尝鲜试用:Web Service调用
- ASP.NET 2.0 AJAX中Webservice调用方法
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(10):服务器端和客户端数据类型的自动转换:以XML方式序列化数据、小结
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法
- ASP.NET 2.0 AJAX中Webservice调用方法示例
- ASP.NET 2.0 AJAX中Webservice调用方法示例
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(7):服务器端和客户端数据类型的自动转换:泛型集合类型
- ASP.NET AJAX调用Web Service
- ASP.NET AJAX1.0:Web Service调用
- ASP.NET 3.5核心编程之AJAX Web Service的调用
- ASP.NET 2.0 AJAX中Webservice调用方法示例
- ASP.NET 3.5核心编程学习笔记:AJAX Web Service的调用
- ASP.NET 2.0 AJAX中Webservice调用方法