scriptmanage js调用后台方法

来源:互联网 发布:uk国家域名缩写 编辑:程序博客网 时间:2024/05/16 19:56

使用PageMehtod的步骤。

a)     启用ASP.NET Ajax,关于如何启用ASP.NET Ajax这里不作叙述;

b)     Web.Config中添加HttpModule: ScriptModule;

Code

<httpModules>

<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

</httpModules>

c)     WebFormCode behind添加WebMethod方法,注意是publicstatic和方法的Attribute;

Code

        [System.Web.Services.WebMethod]

        public static String GetDateTimeNow()

        {

            return System.DateTime.Now.ToString();

        }

 

d)     在相应客户端代码中编写调用Server方法及处理返回结果的两个function;

Code

        function QButton1_onclick()

        {

            PageMethods.GetDateTimeNow(showResult);

        }

        function showResult(result)

        {

            alert(result);

        }

e)     WebForm上的ScriptManager添加属性EnablePageMethods="True";

Code

        function QButton1_onclick()

        {

            PageMethods.GetDateTimeNow(showResult);

        }

        function showResult(result)

        {

            alert(result);

        }

 

2.    关于参数传递。

使用PageMethods调用的方法如下:

PageMethods.[MethodName](parameter1, parameter2, …, SuccessMethod, FailedMethod, userContext);

其中

Parameter?为与Server端一一对应的参数.

SuccessMethod,必需,WebMethod正确返回时的回调函数,原型为onSuccess(result)

FailedMethod,非必需,WebMethod出错返回时的回调函数,原形为onFailed(error)

 

3.    返回值及出错处理。

返回值的获取:

SuccessMethod第一个参数为Server方法返回值.

出错处理:

可能通过提供FailedMethod处理Server抛出的异常, FailedMethod的第一个参数为Server端异常对应的客户端对象,可以通过error.get_message()获取异常信息.


原创粉丝点击