.NET中的pagemethod方法

Atlas一个比较绚丽的功能就是可以通过JS调用ASPX后台的方法,即PageMethod,一般来说处理简单类型还比较容易,我觉得这个功能倒是可以用来处理删除,修改,增加这类的操作,后台处理复杂,返回值就是一个布尔,比较简单,可以作到页面无刷新的效果,所谓温故而知新.
<%@ 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调用的服务端事件";
     }
原文地址:https://www.cnblogs.com/zhangsongshan/p/2352746.html