Asp.net Ajax 客户端异步调用Web Service和当前页的服务器端静态方法

mac2022-06-30  20

Vs2008 包含的asp.net 3.5已经内置了对Ajax的支持,所以你可以用vs2008来建议asp.net 3.5的ajax程序,如果你想用vs2008来开发asp.net 2.0的ajax程序,请在 www.asp.net/ajax上下载扩展包。 一、异步调用Web Service WebService.cs /// <summary> /// Summary description for WebService /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService {

    public WebService () {

        //Uncomment the following line if using designed components         //InitializeComponent();     }

    [WebMethod]     public string HelloWorld(string name) {         return "Hello " + name;     } } Default.aspx 在form中增加如下代码<form id="form1" runat="server">         <asp:ScriptManager ID="ScriptManager1" runat="server">         <Services>         <asp:ServiceReference Path="~/WebService.asmx" />         </Services>         </asp:ScriptManager>                         <input id="tbName" type="text" /> <input id="btnInvoke" type="button" value="Say Hello"     οnclick="return btnInvoke_onclick()" /> <div id="result"></div> </form> 在<Head>中增加<script type="text/javascript" language="javascript">     function btnInvoke_onclick()     {         var name = $get("tbName").value;         WebService.HelloWorld(name,webserviceSuccess);            }     function webserviceSuccess(result)     {        $get("result").innerHTML = result;     }   </script>

总结:想要使用ASP.NET AJAX在客户端JavaScript中异步调用服务器端Web Service,我们需要:

为Web Service类或需要暴露给客户端的Web Service方法添加[ScriptService]属性; 为Web Service中需要暴露给客户端的方法添加[WebMethod]属性; 在页面中的ScriptManager控件中添加对该Web Service的引用; 在客户端使用如下JavaScript语法调用该Web Service: [NameSpace].[ClassName].[MethodName](param1, param2 ......, callbackFunction) 为客户端异步调用指定回调函数,在回调函数中接收返回值并进一步处理。

二、页面方法

Default.cs[System.Web.Services.WebMethod]     public static string SayHelloFromPage(string name)     {         return "hello " + name;     } Default.aspx 在上面的form中增加<input id="Button1" type="button" value="Say Hello"     οnclick="return btnInvoke1_onclick()" /> 在script中增加function btnInvoke1_onclick()     {         var name = $get("tbName").value;         PageMethods.SayHelloFromPage(name,pageSuccess);        }     function pageSuccess(result)     {        $get("result").innerHTML = result;     } 给scriptmanager指定EnablePageMethods属性的值为true. 总结:想要使用ASP.NET AJAX在客户端JavaScript中异步调用定义在ASP.NET页面中的方法,我们需要:

将该方法声明为公有(public); 将该方法声明为类方法(C#中的static,VB.NET中的Shared),而不是实例方法; 为该方法添加[WebMethod]属性; 将页面中ScriptManager控件的EnablePageMethods属性设置为true; 在客户端使用如下JavaScript语法调用该页面方法: PageMethods.[MethodName](param1, param2 …, callbackFunction); 为客户端异步调用指定回调函数,在回调函数中接收返回值并进一步处理。

BTW:vs2008对javascript调用页面的静态方法不提供智能感应支持,所以当你敲入PageMethods.时,没有提示,你不要怀疑这个功能。

shore 2008-01-31 15:36 发表评论

转载于:https://www.cnblogs.com/hotsoho.net/archive/2008/01/31/1206560.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)