下面是一个简单的例子
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static DateTime GetCurrentTime()
{
return DateTime.Now;
}
</script>
<!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>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
<input id="Button1" type="button" value="button" onclick="showit()" />
</div>
<script language="javascript" type="text/javascript">
function showit()
{
PageMethods.GetCurrentTime(suc);
}
function suc(result)
{
alert(result);
}
</script>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static DateTime GetCurrentTime()
{
return DateTime.Now;
}
</script>
<!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>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
<input id="Button1" type="button" value="button" onclick="showit()" />
</div>
<script language="javascript" type="text/javascript">
function showit()
{
PageMethods.GetCurrentTime(suc);
}
function suc(result)
{
alert(result);
}
</script>
</form>
</body>
</html>