<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<script type="text/javascript">
function callServer(){
PageMethods.HelloWorld(OnComplete); //如后面方法有参数则为(参数1,参数2,javascript成功接受值函数名,失败接受值函数名) ,若是调用webservice,则为命名空间.类名.方法体,参数与PageMethods相同.
}
function OnComplete(result){ //result固定,表示后面函数的返回值
document.getElementById('txtName').value = result
}
function onError(error){//error固定,失败后的返回信息
......
}
</script>
<body>
<form id="form1" runat="server">
<atlas:ScriptManager ID="ScriptManager1" runat="server" />
<input type="button" value="这是Html控件" onclick="callServer()" />
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
</form>
</body>
</html>
CS文件:
[System.Web.Services.WebMethod]
public static string HelloWorld() //此处一定要用static
{
return ": 这是JS调用的服务端事件";
}