【转】利用ScriptManager实现Javascript调用WebService中的方法
2017-07-12 00:05
537 查看
原文发布时间为:2009-07-01 —— 来源于本人的百度文章 [由搬家工具导入]
前台调用后台方法,或者后台调用前台方法。ScriptManager实现
开发过程中,总想在前台直接调用后台的method,其原因无非是想控制页面刷新,改善用户体验。可以达到这个目的的方法有很多:Ajax应该是用的最多的情况.这里要说的是利用Javascript和webservice的交互来达到目的。
1.当然要建立WebService(简便起见,WebService就建立在当前方案中):
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
[System.Web.Script.Services.ScriptService] //注意要实现目的,这就必不可少
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services >
<asp:ServiceReference Path ="~/WebService.asmx" />
</Services>
</asp:ScriptManager>
<input id="txtname" name="txtname" type="text" />
<input id="txtage" name="txtage" type="text" />
<input id="Button1" type="button" value="button" onclick ="check()" />
</div>
</form>
</body>
</html>
3.说明:
除了上述两点需要注意外,还有一点需要强调一下:
调用的方法很,[WebServiceName].[MethodName](...),其中的参数列中最后一项应该为回调函数名称。而回调函数的参数既是回传的结果。见上例。
使用这些ajax控件,sys未定义,则要在web.config的 <system.web>节点下面添加以下语句:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
范例二:
在工程中添加一个 webservice 使用默认的
红色部分为后添加
为了要让ScriptManager识别这个WEBSERVICE
WebService.cs
using System;using System.Web;using System.Collections;using System.Web.Services;using System.Web.Services.Protocols; /// <summary>/// WebService 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService ]public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod]public string HelloWorld() {return "Hello World";} } 写好后在 ASPX中写入 脚本代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"><title>无标题页</title><script type ="text/javascript" >function RefService(){WebService.HelloWorld(GetResult);}function GetResult(result){alert(result);} </script></head><body><form id="form1" runat="server"><div><asp:ScriptManager ID="ScriptManager1" runat="server"> <Services><asp:ServiceReference Path="WebService.asmx"/></Services></asp:ScriptManager><input type="button" value="abc" onclick="RefService()" /></div></form></body></html>
这个为添加的
<Services><asp:ServiceReference Path="WebService.asmx"/></Services>然后 添加一个 Button 调用 一下 RefService() 方法
前台调用后台方法,或者后台调用前台方法。ScriptManager实现
开发过程中,总想在前台直接调用后台的method,其原因无非是想控制页面刷新,改善用户体验。可以达到这个目的的方法有很多:Ajax应该是用的最多的情况.这里要说的是利用Javascript和webservice的交互来达到目的。
1.当然要建立WebService(简便起见,WebService就建立在当前方案中):
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
[System.Web.Script.Services.ScriptService] //注意要实现目的,这就必不可少
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>未命名頁面</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services >
<asp:ServiceReference Path ="~/WebService.asmx" />
</Services>
</asp:ScriptManager>
<input id="txtname" name="txtname" type="text" />
<input id="txtage" name="txtage" type="text" />
<input id="Button1" type="button" value="button" onclick ="check()" />
</div>
</form>
</body>
</html>
3.说明:
除了上述两点需要注意外,还有一点需要强调一下:
调用的方法很,[WebServiceName].[MethodName](...),其中的参数列中最后一项应该为回调函数名称。而回调函数的参数既是回传的结果。见上例。
使用这些ajax控件,sys未定义,则要在web.config的 <system.web>节点下面添加以下语句:
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
范例二:
在工程中添加一个 webservice 使用默认的
红色部分为后添加
为了要让ScriptManager识别这个WEBSERVICE
WebService.cs
using System;using System.Web;using System.Collections;using System.Web.Services;using System.Web.Services.Protocols; /// <summary>/// WebService 的摘要说明/// </summary>[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)][System.Web.Script.Services.ScriptService ]public class WebService : System.Web.Services.WebService { public WebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod]public string HelloWorld() {return "Hello World";} } 写好后在 ASPX中写入 脚本代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"><title>无标题页</title><script type ="text/javascript" >function RefService(){WebService.HelloWorld(GetResult);}function GetResult(result){alert(result);} </script></head><body><form id="form1" runat="server"><div><asp:ScriptManager ID="ScriptManager1" runat="server"> <Services><asp:ServiceReference Path="WebService.asmx"/></Services></asp:ScriptManager><input type="button" value="abc" onclick="RefService()" /></div></form></body></html>
这个为添加的
<Services><asp:ServiceReference Path="WebService.asmx"/></Services>然后 添加一个 Button 调用 一下 RefService() 方法
相关文章推荐
- 利用ScriptManager实现Javascript调用WebService中的方法
- 利用ScriptManager实现Javascript调用WebService中的方法
- 利用ScriptManager实现Javascript调用WebService中的方法
- iOS JS 交互之利用系统JSContext实现 JS调用OC方法以及Objective-C调用JavaScript方法
- 通过ScriptManager在客户端来调用服务器端方法或者webService的方法
- javascript 调用 WebService的方法
- 利用javascript实现web页面刷新的方法
- javascript 调用 WebService的方法
- javascript 调用 WebService的方法
- 实现vbscript和javascript互相调用方法
- 利用Anthem.net 实现前台javascript调用服务器端c#函数 及流程分析
- javascript调用webservice实现base64加密
- [转]通过ScriptManager在客户端来调用服务器端方法或者webService的方法
- javascript 调用 WebService 的方法
- 利用Windows Group实现基于角色的WCF接口方法调用授权
- Javascript调用Webservice的多种方法
- 利用Anthem.net 实现前台javascript调用服务器端c#函数 及流程分析
- asp.net下利用JS实现对后台CS代码的调用方法
- [Silverlight探秘]利用Silverlight调用Javascript的方法
- ScriptManager在客户端来调用服务器端方法或者webService的方法