JS调用asp.net后台方法:PageMethods

先帮朋友宣传一下程序人生(http://www.manong123.com)的网站,里面都是开发感悟,开发人员创业,支持一下吧~

原来是通过PageMethods来实现的。

举个列子:

Default.aspx 里代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>无标题页</title> 
<script type="text/javascript" language="javascript"> 
<!-- 
function minbzdm() 
{ 
PageMethods.OK(xxx); 
} 
function xxx(result) 
{ 
alert(result); 
} 
//--> 
</script> 
</head> 
<body> 
    <form  runat="server"> 
    <asp:ScriptManager  runat="server" EnablePageMethods="true"> 
    </asp:ScriptManager> 
    <div> 
    <input type='button' value='删除' onclick='minbzdm()' /> 
    </div> 
    </form> 
</body> 
</html> 

Default.aspx.cs里的代码

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
     { 
     } 

     [System.Web.Services.WebMethod] 
    public static string OK() 
     { 
        return "OK"; 
     } 
} 
public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
     { 
     } 

     [System.Web.Services.WebMethod] 
    public static string OK() 
     { 
        return "OK"; 
     } 
} 

通过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:

function check() { 

            var title = document.getElementById("TabContainer1_tabpanel4_txtPicTitle").value; 

            var sn = document.getElementById("TabContainer1_tabpanel4_txtSN").value; 

            var playGUID = document.getElementById("TabContainer1_tabpanel4_hidPlayGUID").value; 

            var msg = Drilling_Pictures.Validate(title, sn, playGUID).value;//主要注意这里 

            if (msg == "") { 

                return true; 

            } else { 

                alert(msg); 

                return false; 

            } 

        } 

<asp:ImageButton  runat="server" ImageUrl="~/images/btn25.png" OnClick="imgBtn_Click"                                                               OnClientClick="return check();" /> 

后台代码:

public partial class Drilling_Pictures : BasePage 

{ 

    [AjaxPro.AjaxMethod] 

    public string Validate(string title, string sn, string playGUID) 

    { 

        string str = ""; 

        if (title.Trim() == "") 

        { 

            str = "标题不能为空"; 

        } 

        else if (sn.Trim() == "") 

        { 

            str = "序号不能为空"; 

        } 

        else if (!ConvertInt(sn.Trim())) 

        { 

            str = "序号必须为整数"; 

        } 

        else if (!new DrillingPlayBack().CheckSN(playGUID, int.Parse(sn))) 

        { 

            str = "序号已经存在"; 

        } 

        return str; 

    } 

}