Si parla di AJAX.
Un PageMethod è un metodo lato server invocabile tramite Javascript e l'ausilio di uno ScriptManager con EnablePageMethods="true".
Ci vogliono altri due metodi Javascript di callback per la gestione della risposta e dell'errore:
ASPX
codice:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
function GetDate() {
PageMethods.GetDate('Data ed ora attuali: ', OnComplete, OnError);
}
function OnComplete(result, userContext, methodName) {
alert(result);
}
function OnError(error, userContext, methodName) {
if (error != null) {
alert(error.get_message());
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="sm" EnablePageMethods="true" runat="server"></asp:ScriptManager>
<input type="button" value="Click per invocare il WebMethod" onclick="GetDate()" />
</div>
</form>
</body>
</html>
C#
codice:
[System.Web.Services.WebMethod]
public static string GetDate(string text)
{
return text + "-" + DateTime.Now.ToString();
}
Un WebMethod invece è per definizione un metodo di un WebService (windows communication foundation oppure i "vecchi" asmx)