using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
// 需要标识为WebMethod
[System.Web.Services.WebMethod]
//[System.Web.Services.WebService]
// 注意,要让前台调用的方法,一定要是public和static的
public static string Hello(string name)
{
return "Hello:" + name;
}
}
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function btnClick(){
// 调用页面后台方法,前面跟方法所需的参数,接着是方法回调成功时要执行的js函数,最后一个是方法回调失败时要执行的js函数
PageMethods.Hello("you",funReady,funError);
}
// result 就是后台方法返回的数据
function funReady(result){
alert(result);
}
// err 就是后台方法返回的错误信息
function funError(err){
alert("Error:" + err._message );
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
下面要加上EnablePageMethods="true"属性,才能使用后台方法
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<input type="button" οnclick="btnClick();" value="test" />
</div>
</form>
</body>
</html>