ASP.NET AJAX

来源:互联网 发布:信佑linux无盘 编辑:程序博客网 时间:2024/05/18 03:40
  1. namespace AJAX1
  2. {
  3.     public partial class _Default : System.Web.UI.Page
  4.     {
  5.         // 需要标识为WebMethod 
  6.         [System.Web.Services.WebMethod]
  7.         // 注意,要让前台调用的方法,一定要是public和static的 
  8.         public static string Hello(string name)
  9.         {
  10.             return "Hello:" + name;
  11.         }
  12.     }
  13. }


  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AJAX1._Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" >
  4. <head runat="server">
  5.     <title>Untitled Page</title>
  6.     <script type="text/javascript">
  7.         function btnClick(){
  8.             // 调用页面后台方法,前面跟方法所需的参数,接着是方法回调成功时要执行的js函数,最后一个是方法回调失败时要执行的js函数
  9.             PageMethods.Hello("you",funReady,funError);
  10.         }        
  11.         // result 就是后台方法返回的数据
  12.         function funReady(result){
  13.             alert(result);
  14.         }
  15.         // err 就是后台方法返回的错误信息
  16.         function funError(err){
  17.             alert("Error:" + err._message );
  18.         }
  19.     </script>
  20. </head>
  21. <body>
  22.     <form id="form1" runat="server">
  23.     <div>
  24.         下面要加上EnablePageMethods="true"属性,才能使用后台方法        
  25.         <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
  26.         </asp:ScriptManager>
  27.         <input type="button" onclick="btnClick();" value="test" />
  28.     </div>
  29.     </form>
  30. </body>
  31. </html>
转载自:http://blog.csdn.net/youbl/article/details/2837884

0 0
原创粉丝点击