使用 AjaxManager 生成调用服务器端方法的 javascript 函数
2011-12-10 12:25
721 查看
[b]通[/b]过 AjaxManager, 我们可以方便的生成调用 WebService 或者一般处理程序的 javascript 函数, 这样就可以方便的在客户端调用.
本文更新:
2011-12-12: 去掉 ParameterList 和 AjaxList.
由于精力有限, 不能在多个博客中保证文章的同步, 可在如下地址查看最新内容, 请谅解:
http://code.google.com/p/zsharedcode/wiki/AjaxManager
请到 Download 下载资源 的 JQueryElement 示例下载一节下载示例代码
本文将说明如何使用 AjaxManager 来生成调用服务器端方法的 javascript 函数, 以及如何调用这些函数:
* 准备
* 创建 javascript 函数
* 直接调用
* 通过 Async 属性调用
* 隐式添加的参数
![](http://pic002.cnblogs.com/images/2011/273838/2011121012144054.jpg)
[b]准备[/b]
请确保已经在 Download 下载资源 中下载 JQueryElement 最新的版本.
请使用指令引用如下的命名空间:
除了命名空间, 还需要引用 jQueryUI 的脚本:
[b]创建 javascript 函数[/b]
在页面中添加一个 AjaxManager 控件, 来创建调用服务器端方法的 javascript 函数:
上面的示例, 生成了一个名为 add 的 javascript 函数, 在此函数中将调用一般处理程序 handler.ashx 来返回 JSON 数据.
代码中的 -:data 将被替换为 data 或者 data.d, 更多内容请参考 使用 ASP.NET 一般处理程序或 WebService 返回 JSON.
通过 Parameter 对象可以为 Ajax 调用增加参数, 详细内容请参考 通过 Parameter 对象添加 Ajax 请求时的参数.
[b]设置 javascript 函数的参数列表[/b]
通过 ClientParameter 属性, 可以为 javascript 函数设置参数列表:
上面的示例中, 为 add3 函数增加了一个 othernum 参数, 而 othernum 参数将作为 num3 的值传递给服务器端. 可以像这样来调用 add3:
[b]直接调用[/b]
在上面的例子中, 已经展示了直接调用, 就和调用普通的 javascript 函数是一样的:
[b]通过 Async 属性调用[/b]
对于 JQueryElement 的控件可以通过 Async 属性来调用 AjaxManager 生成的函数:
通过 Async 的 AjaxManagerID 来指定需要调用的 javascript 函数所在的 AjaxManager, 通过 ClientFunction 来指定调用的 javascript 函数名称.
[b]隐式添加的参数[/b]
部分 JQueryElement 控件会为 AjaxManager 增加 Parameter 对象, 比如 Repeater 会增加 pageindex, pagesize 等:
虽然 AjaxManager 中的 fill 函数没有添加任何的 Parameter, 但由于 Repeater 的 FillAsync 被指定调用 fill 函数, 因此 fill 函数被隐式的添加 pageindex, pagesize 等参数.
JQueryElement 是开源共享的代码, 可以在 http://code.google.com/p/zsharedcode/wiki/Download 页面下载 dll 或者是源代码.
实际过程演示: http://www.tudou.com/programs/view/Bbk5GvsEGKs/, 建议全屏观看.
本文更新:
2011-12-12: 去掉 ParameterList 和 AjaxList.
由于精力有限, 不能在多个博客中保证文章的同步, 可在如下地址查看最新内容, 请谅解:
http://code.google.com/p/zsharedcode/wiki/AjaxManager
请到 Download 下载资源 的 JQueryElement 示例下载一节下载示例代码
本文将说明如何使用 AjaxManager 来生成调用服务器端方法的 javascript 函数, 以及如何调用这些函数:
* 准备
* 创建 javascript 函数
* 直接调用
* 通过 Async 属性调用
* 隐式添加的参数
![](http://pic002.cnblogs.com/images/2011/273838/2011121012144054.jpg)
[b]准备[/b]
请确保已经在 Download 下载资源 中下载 JQueryElement 最新的版本.
请使用指令引用如下的命名空间:
<%@ Register Assembly="zoyobar.shared.panzer.JQueryElement" Namespace="zoyobar.shared.panzer.ui.jqueryui" TagPrefix="je" %> <%@ Register Assembly="zoyobar.shared.panzer.JQueryElement" Namespace="zoyobar.shared.panzer.web.jqueryui" TagPrefix="je" %>
除了命名空间, 还需要引用 jQueryUI 的脚本:
<script type="text/javascript" src="[脚本路径]/jquery-<version>.min.js"></script>
[b]创建 javascript 函数[/b]
在页面中添加一个 AjaxManager 控件, 来创建调用服务器端方法的 javascript 函数:
<je:AjaxManager ID="manager" runat="server"> <AjaxList> <je:AjaxSetting ClientFunction="<javascript 函数名>" ClientParameter="<javascript 参数, 比如: name, age>" Url="<服务器端方法地址>" MethodName="<服务器端方法名称>" Success="<调用成功时的 javascript 函数>" Error="<调用失败时的 javascript 函数>" Complete="<调用完成时的 javascript 函数>" ... > <ParameterList> <参数> </ParameterList> </je:AjaxSetting> </AjaxList> </je:AjaxManager> <je:AjaxManager ID="manager" runat="server"> <AjaxList> <je:AjaxSetting ClientFunction="add" Url="handler.ashx" Success=" function(data){ $('#result').text(-:data.result); } "> <ParameterList> <je:Parameter Name="c" Type="Expression" Value="'add'" /> <je:Parameter Name="num1" Type="Selector" Value="'#num1'" DataType="Number" /> <je:Parameter Name="num2" Type="Selector" Value="'#num2'" DataType="Number" /> </ParameterList> </je:AjaxSetting> </AjaxList> </je:AjaxManager>
上面的示例, 生成了一个名为 add 的 javascript 函数, 在此函数中将调用一般处理程序 handler.ashx 来返回 JSON 数据.
代码中的 -:data 将被替换为 data 或者 data.d, 更多内容请参考 使用 ASP.NET 一般处理程序或 WebService 返回 JSON.
通过 Parameter 对象可以为 Ajax 调用增加参数, 详细内容请参考 通过 Parameter 对象添加 Ajax 请求时的参数.
[b]设置 javascript 函数的参数列表[/b]
通过 ClientParameter 属性, 可以为 javascript 函数设置参数列表:
<je:AjaxManager ID="manager" runat="server"> <AjaxList> <je:AjaxSetting ClientFunction="add3" ClientParameter="othernum" Url="handler.ashx" ... > <ParameterList> <je:Parameter Name="num3" Type="Expression" Value="othernum" /> </ParameterList> </je:AjaxSetting> </AjaxList> </je:AjaxManager>
上面的示例中, 为 add3 函数增加了一个 othernum 参数, 而 othernum 参数将作为 num3 的值传递给服务器端. 可以像这样来调用 add3:
<input type="button" onclick="javascript:add3(1);" value="额外加 1" />
[b]直接调用[/b]
在上面的例子中, 已经展示了直接调用, 就和调用普通的 javascript 函数是一样的:
<script> $(function () { add3(1); }); </script>
[b]通过 Async 属性调用[/b]
对于 JQueryElement 的控件可以通过 Async 属性来调用 AjaxManager 生成的函数:
<je:Button ID="cmdSub" runat="server" IsVariable="true" Label="减" Disabled="true" ClickAsync-AjaxManagerID="manager" ClickAsync-ClientFunction="sub"> </je:Button>
通过 Async 的 AjaxManagerID 来指定需要调用的 javascript 函数所在的 AjaxManager, 通过 ClientFunction 来指定调用的 javascript 函数名称.
[b]隐式添加的参数[/b]
部分 JQueryElement 控件会为 AjaxManager 增加 Parameter 对象, 比如 Repeater 会增加 pageindex, pagesize 等:
<je:Repeater ID="repeater" runat="server" FillAsync-AjaxManagerID="manager" FillAsync-ClientFunction="fill"> </je:Repeater> <je:AjaxManager ID="manager" runat="server"> <AjaxList> <je:AjaxSetting ClientFunction="fill" ClientParameter="othernum" Url="handler.ashx" ... > <ParameterList> </ParameterList> </je:AjaxSetting> </AjaxList> </je:AjaxManager>
虽然 AjaxManager 中的 fill 函数没有添加任何的 Parameter, 但由于 Repeater 的 FillAsync 被指定调用 fill 函数, 因此 fill 函数被隐式的添加 pageindex, pagesize 等参数.
JQueryElement 是开源共享的代码, 可以在 http://code.google.com/p/zsharedcode/wiki/Download 页面下载 dll 或者是源代码.
实际过程演示: http://www.tudou.com/programs/view/Bbk5GvsEGKs/, 建议全屏观看.
相关文章推荐
- eclipse 中main()函数中的String[] args如何使用?通过String[] args验证账号密码的登录类?静态的主方法怎样才能调用非static的方法——通过生成对象?在类中制作一个方法——能够修改对象的属性值?
- JavaScript的函数及调用方法
- javascript两种禁止一个函数没有运行结束时就再次调用的方法
- 在Angular外部使用js调用Angular控制器中提供的函数方法或变量
- javascript中eval()函数使用方法
- 使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明
- JavaScript调用堆栈及setTimeout使用方法深入剖析
- call 方法在使用一个指定的this值和若干个指定的参数值的前提下调用某个函数或方法.
- 【原创】PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)
- PB调用SAP的Web Service结构体数组参数方法详解(使用C#调用Web Service生成dll)
- [NET]Net中的反射使用入门(根据类名和函数名,生成和调用对象的成员函数) .
- asp.net javascript客户端调用服务器端方法
- PHP中函数参数的调用及其灵活使用方法
- Android使用ksoap2调用C#中的webservice函数方法
- javascript中iframe里面的页面调用父窗口js函数的方法。
- 使用Signature Tool自动生成P/Invoke调用Windows API的C#函数声明
- javascript中函数作为参数调用的方法
- 转载:JavaScript 调用后台方法和函数的方法
- [Effective JavaScript 笔记] 第13条:使用立即调用的函数表达式创建局部作用域
- 怎样在前端Javascript中调用C#方法(3)使用特性Attribute