利用WebServices实现客户端调用WebServices里的方法和Page的方法。
随后自己做了个Demo如下:
<%@ 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>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods="true"
>
<Scripts>
<asp:ScriptReference Path="~/JScript.js" />
</Scripts>
</asp:ScriptManager>
<input type="button" value="Get Current Time" onclick="getCurrentTime()" />
<br />
<input type="button" value="Set Label Name" onclick="setLabelName()" />
<div id="dv1"></div>
<script language="javascript" type="text/javascript" >
function getCurrentTime() {
PageMethods.GetCurrentTime(getCurrentTimeSucceeded);
}
function setLabelName() {
PageMethods.SetLabelName("DemoLabel", getLabelNameSucceeded);
}
function getCurrentTimeSucceeded(result) {
alert(result);
}
function getLabelNameSucceeded(result) {
document.getElementById("dv1").innerHTML = result;
}
</script>
</form>
</body>
</html>
ScriptManager 控件中需要把EnablePageMethods属性设为true,这样才能将事件代理写到页面上,利用JavaScript脚本调用。
using System.Web.Services;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static DateTime GetCurrentTime()
{
return DateTime.Now;
}
[WebMethod]
public static string SetLabelName(string name)
{
return name;
}
}