asp.net ajax动态显示时间

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>   
  2.   
  3.   
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">   
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">   
  8.   
  9. <head runat="server">   
  10.   
  11.     <title>ajax动态显示时间 </title>   
  12.   
  13.     <script language="javascript" type="text/javascript">   
  14.   
  15.         function btnInvoke_onclick()   
  16.   
  17.         {   
  18.   
  19.             var txtName=$get("txtName").value;   
  20.   
  21.             //var txtName=document.getElementById("txtName").value;   
  22.   
  23.             PageMethods.SayHello(txtName,SayHelloShow);   
  24.   
  25.         }   
  26.   
  27.         //回调函数   
  28.   
  29.        function SayHelloShow(res)   
  30.   
  31.         {   
  32.   
  33.           $get("result").innerHTML=res;   
  34.   
  35.           //document.getElementById("result").innerHTML=res;   
  36.   
  37.         }   
  38.   
  39.         function TimeCall()   
  40.   
  41.         {   
  42.   
  43.             setInterval("btnInvoke_onclick()",1000);   
  44.   
  45.             //setTimeout("btnInvoke_onclick()",1000);   
  46.   
  47.         }   
  48.   
  49.     </script>   
  50.   
  51. </head>   
  52.   
  53. <body onload="TimeCall()">   
  54.   
  55.     <form id="form1" method="get" runat="server">   
  56.   
  57.         <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"  />   
  58.   
  59.         <input type="text" id="txtName" value="动态时间 " />   
  60.   
  61.         <input type="button" id="btnInvoke" value="测试" onclick="return btnInvoke_onclick()" />   
  62.   
  63.         <div id="result" style="color:Red;" ></div>   
  64.   
  65.         <br />   
  66.   
  67.         <textarea id="TraceConsole" rows="10" cols="50" />   
  68.   
  69.     </form>   
  70.   
  71. </body>   
  72.   
  73. </html>   
  74.   
  75.   
  76.   
  77.   
  78.   
  79. using System;   
  80.   
  81. using System.Data;   
  82.   
  83. using System.Configuration;   
  84.   
  85. using System.Web;   
  86.   
  87. using System.Web.Security;   
  88.   
  89. using System.Web.UI;   
  90.   
  91. using System.Web.UI.WebControls;   
  92.   
  93. using System.Web.UI.WebControls.WebParts;   
  94.   
  95. using System.Web.UI.HtmlControls;   
  96.   
  97. using System.Web.Services;   
  98.   
  99.   
  100.   
  101. public partial class _Default : System.Web.UI.Page    
  102.   
  103. {   
  104.   
  105.     protected void Page_Load(object sender, EventArgs e)   
  106.   
  107.     {   
  108.   
  109.   
  110.   
  111.     }   
  112.   
  113.   
  114.   
  115.     [WebMethod]   
  116.   
  117.     public static string SayHello(string name)   
  118.   
  119.     {   
  120.   
  121.         return string.Format("你好{0}!现在时间:{1}", name, DateTime.Now.ToString());   
  122.   
  123.     }   
  124.   
  125. }   
  126.   
  127.   
  128.   
  129.   
  130.   
  131. 注意:   
  132.   
  133. 1 <asp:ScriptManager ID="sm" runat="server" EnablePageMethods="true"  />中的EnablePageMethods   
  134.   
  135. 要为true,表示客户端页面能直接调用服务器端页面的静态方法.   
  136.   
  137.   
  138.   
  139. 2    
  140.   
  141.     [WebMethod]   
  142.   
  143.   
  144.   
  145.     public static string SayHello(string name)   
  146.   
  147.     {   
  148.   
  149.   
  150.   
  151.         return string.Format("你好{0}!现在时间:{1}", name, DateTime.Now.ToString());   
  152.   
  153.   
  154.   
  155.     }   
  156.   
  157.     必须是静态方法.必须加[WebMethod]   
  158.   
  159.   
  160.   
  161. 3 PageMethods.SayHello(txtName,SayHelloShow);   
  162.   
  163.   表示服务器页面方法中的SayHello,SayHelloShow表示回调函数,主要用于前台显示  
原文地址:https://www.cnblogs.com/liufei88866/p/1255808.html