VS2008中JS调用后台方法需要注意:
在需要调用的后台方法前面引用[System.Web.Services.WebMethod]或直接[WebMethod]即可.
示例代码:
后台方法:
[System.Web.Services.WebMethod]
public static string ShowMsg(string str)
{
string msg = "Hello World"+str;
return msg;
}
前面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CallPostBack.aspx.cs" Inherits="FreeProject.CallPostBack" %>
<!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>Untitled Page</title>
<script>
function GetServerMsg_Click()
{
PageMethods.ShowMsg("aaa",cb_getServerMsg);
}
function cb_getServerMsg(result)
{
document.getElementById("result").innerHTML = result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">
</asp:ScriptManager>
<div>
<input type="button" value="Get Server Message" id="btnGetServerMsg" οnclick="return GetServerMsg_Click()"/>
<span id="result" />
</div>
</form>
</body>
</html>
(表达能力有限:应该写得还是可以看得懂哦)