JS调用CS里的方法:WebMethod PageMethods AjaxMethod

来源:互联网 发布:linux 连接oracle 编辑:程序博客网 时间:2024/05/21 07:48
 

http://blog.csdn.net/llll29550242/article/details/6120426举个列子:

Default.aspx 里代码

Default.aspx 里代码
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7. <title>无标题页</title>  
  8. <mce:script type="text/javascript" language="javascript"><!--  
  9. function minbzdm()  
  10. {  
  11. PageMethods.OK(xxx);  
  12. }  
  13. function xxx(result)  
  14. {  
  15. alert(result);  
  16. }  
  17. // --></mce:script>  
  18. </head>  
  19. <body>  
  20.     <form id="form1" runat="server">  
  21.     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">  
  22.     </asp:ScriptManager>  
  23.     <div>  
  24.     <input type='button' value='删除' onclick='minbzdm()' />  
  25.     </div>  
  26.     </form>  
  27. </body>  
  28. </html>  
  29.   
  30. Default.aspx.cs里的代码  
  31.   
  32. public partial class _Default : System.Web.UI.Page  
  33. {  
  34.     protected void Page_Load(object sender, EventArgs e)  
  35.      {  
  36.      }  
  37.   
  38.      [System.Web.Services.WebMethod]  
  39.     public static string OK()   
  40.      {  
  41.         return "OK";  
  42.      }  
  43. }  

 

 

本实例经过测试,在vs2010的服务器上运行可能会出现错误,最好是在IIS上运行

 

通过PageMethods方法来实现JS调用CS,必须注意一下几点:

【1】静态的方法

          public static

 

【2】需要在cs方法上加上:

         [System.Web.Services.WebMethod]

 

【3】需要自定义一个函数接受结果

        function xxx(result)
        {
        alert(result);
        }

 

【4】ScriptManager 必须设置成 EnablePageMethods="true"

注意:

1、PageMethods.OK( 参数一,参数二,参数三,...,xxx);可以传入参数。对应的CS后台方法也要改进!

2、xxx为回调函数名,只有仅有一个参数。参数可以是数组,List<T>泛型等。

http://www.cnblogs.com/juxiaoqi/archive/2008/08/21/1273059.html

补充:

AJAX 调用Web Service 与 PageMethods注意细节

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

1 为Web Service类或需要暴露给客户端的Web Service方法添加[ScriptService]
      属性;

2 为Web Service中需要暴露给客户端的方法添加[WebMethod]属性;

3 在页面中的ScriptManager控件中添加对该Web Service的引用;

4 在客户端使用如下JavaScript语法调用该Web Service:
      [NameSpace].[ClassName].[MethodName](param1, param2,..., callbackFunction)

5 为客户端异步调用指定回调函数,在回调函数中接收返回值并进一步处理。


-----------------------

想要使用ASP.NET AJAX在客户端JavaScript中异步调用定义在ASP.NET页面中的方法,我们需要:

1 将该方法声明为公有(public);

2 将该方法声明为类方法(C#中的static,VB.NET中的Shared),而不是实例方法;

3 为该方法添加[WebMethod]属性;

4 将页面中ScriptManager控件的EnablePageMethods属性设置为true;

5 在客户端使用如下JavaScript语法调用该页面方法:
      PageMethods.[MethodName](param1, param2,..., callbackFunction);

6 为客户端异步调用指定回调函数,在回调函数中接收返回值并进一步处理。

补充:另外一种调用

 

前台JS:

view plaincopy to clipboardprint?
  1. function check() {  
  2.   
  3.             var title = document.getElementById("TabContainer1_tabpanel4_txtPicTitle").value;  
  4.   
  5.             var sn = document.getElementById("TabContainer1_tabpanel4_txtSN").value;  
  6.   
  7.             var playGUID = document.getElementById("TabContainer1_tabpanel4_hidPlayGUID").value;  
  8.   
  9.             var msg = Drilling_Pictures.Validate(title, sn, playGUID).value;//主要注意这里   
  10.   
  11.             if (msg == "") {  
  12.   
  13.                 return true;  
  14.   
  15.             } else {  
  16.   
  17.                 alert(msg);  
  18.   
  19.                 return false;  
  20.   
  21.             }  
  22.   
  23.         }  
  24.   
  25. <asp:ImageButton ID="imgBtn" runat="server" ImageUrl="~/images/btn25.png" OnClick="imgBtn_Click"  
  26.   
  27.                                                                 OnClientClick="return check();" />  
  

后台代码:

view plaincopy to clipboardprint?
  1. public partial class Drilling_Pictures : BasePage  
  2.   
  3. {  
  4.   
  5.     [AjaxPro.AjaxMethod]  
  6.   
  7.     public string Validate(string title, string sn, string playGUID)  
  8.   
  9.     {  
  10.   
  11.         string str = "";  
  12.   
  13.         if (title.Trim() == "")  
  14.   
  15.         {  
  16.   
  17.             str = "标题不能为空";  
  18.   
  19.         }  
  20.   
  21.         else if (sn.Trim() == "")  
  22.   
  23.         {  
  24.   
  25.             str = "序号不能为空";  
  26.   
  27.         }  
  28.   
  29.         else if (!ConvertInt(sn.Trim()))  
  30.   
  31.         {  
  32.   
  33.             str = "序号必须为整数";  
  34.   
  35.         }  
  36.   
  37.         else if (!new DrillingPlayBack().CheckSN(playGUID, int.Parse(sn)))  
  38.   
  39.         {  
  40.   
  41.             str = "序号已经存在";  
  42.   
  43.         }  
  44.   
  45.         return str;  
  46.   
  47.     }  
  48.   
  49. }