上次更改后在页面生成以下代码
1 <script type="text/javascript">
2 var WebForm1 =function(){};
3 WebForm1.TestMethod =function(x,y,clientCallBack){
4 JqAjax.Post("test.WebForm1","TestMethod",[x,y],clientCallBack,"/WebForm1.aspx?Ajax=true");}
5 </script>
6
2 var WebForm1 =function(){};
3 WebForm1.TestMethod =function(x,y,clientCallBack){
4 JqAjax.Post("test.WebForm1","TestMethod",[x,y],clientCallBack,"/WebForm1.aspx?Ajax=true");}
5 </script>
6
以上的代码很容易就构成了一个JS类的包装,上次谈过把所有的方法都动态生成了以上的方法,然后输出到页面。在需要的时候就可以如下调用
<input id="button3" type="button" value="ajaxbutton" onclick="WebForm1.TestMethod(3,4,callback)"/>
JqAjax.Post是自定义的一个的小插件
代码
1 var JqAjax =new Object();
2
3 (function($){
4 $.extend(JqAjax, {
5 Post:function(serverType, serverMethod, args, clientCallBack, url)
6 {
7 var dataBuilder =new StringBuilder();
8 //js StringBuilder可查网上资料
9 dataBuilder.append( "Type=" );
10 dataBuilder.append( Type );
11 dataBuilder.append( "&AMethod=" );
12 dataBuilder.append( Method );
13 if ( args ) {
14 for ( var i =0, count = args.length; i < count; i++ ) {
15 dataBuilder.append( "&Arg" );
16 dataBuilder.append( i );
17 dataBuilder.append( "=" );
18 dataBuilder.append( ( args[i] ) );}
19 }
20 $.post(url,dataBuilder.toString(),clientCallBack,"json");
21 }
22 })
23
24 })(jQuery);
2
3 (function($){
4 $.extend(JqAjax, {
5 Post:function(serverType, serverMethod, args, clientCallBack, url)
6 {
7 var dataBuilder =new StringBuilder();
8 //js StringBuilder可查网上资料
9 dataBuilder.append( "Type=" );
10 dataBuilder.append( Type );
11 dataBuilder.append( "&AMethod=" );
12 dataBuilder.append( Method );
13 if ( args ) {
14 for ( var i =0, count = args.length; i < count; i++ ) {
15 dataBuilder.append( "&Arg" );
16 dataBuilder.append( i );
17 dataBuilder.append( "=" );
18 dataBuilder.append( ( args[i] ) );}
19 }
20 $.post(url,dataBuilder.toString(),clientCallBack,"json");
21 }
22 })
23
24 })(jQuery);
在post后,只要在Handle中截取到是ajax的传递如上面可过来判定Ajax=true或自定义扩展名,反射(反射的使用网上很多)到具体的方法 methodInfo.Invoke就动态可运行一个方法,返回时注意将 HttpResponse.ContentType = "application/x-javascript";就ok了。