ajax

aspx页面
<%@ 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 http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml">
<head runat="server">
   
<title>Untitled Page</title>
   
<script type="text/javascript">
    function show(ob)
{
   
var str="测试"; PageMethods.Hello(str,funReady,funError); //这里既调用后台方法,hello既后台方法,一个参数传递的值,第二个为成功的回调函数,第三个为发生错误的回调函数
}
       function funReady(result){
     
var outhtml="<table>";
      
for(var i=0;i<result.length;i++)
       {
       outhtml
+="<tr>";
       outhtml
+="<td>"+result+"</td>";
       outhtml
+="</tr>"
       }
       document.getElementById(
'change').outerHTML=outhtml;
        
        }
       
// err 就是后台方法返回的错误信息
        function funError(err){
            alert(
"Error:"+ err._message );
        }
</script>
</head>
<body>
   
<form id="form1" runat="server">
       
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>//这个属性也是关键
        <div id="change">
           
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
           
<ContentTemplate>
               
<asp:Button ID="Button1"  runat="server" Text="Button"    onmouseOver="show()"/>
           
</ContentTemplate>
           
</asp:UpdatePanel>
       
</div>
   
</form>
</body>
</html>
后置代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
publicpartialclass _Default : System.Web.UI.Page
{
   
protectedvoid Page_Load(object sender, EventArgs e)
    {
        
    }
  [System.Web.Services.WebMethod]
//方法需要加上的特性
   
// 注意,要让前台调用的方法,一定要是public和static的  
   publicstatic List<string>  Hello(string name)
    {
           List
<string> list=new List<string>();
          list.Add(
"111");
          list.Add(
"2222");
          list.Add(
"2333");
         
return list;
    }
  
}

原文地址:https://www.cnblogs.com/renzhendewo/p/2289740.html