用Ajax实现获取服务器上的时间的简单的例子
首先,要先引用一个类库AjaxPro2.dll,
然后再页面的后台代码中注册Ajax,并做一个ServerService的类,并在类的方法上贴上[AjaxMethod]的标签
AjaxPro.Utility.RegisterTypeForAjax(typeof(ServerService));//注册
ServerService类:
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;
/// <summary>
/// ServerService 的摘要说明
/// </summary>
[System.Serializable]
public class SelectItem
{
    private string value;
private string text;
 public string Text
 {
  get { return text;}
  set { text = value;}
 }
 public string Value
 {
  get { return value;}
  set { value = value;}
 }
}
public class ServerService
{
 public ServerService()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }
    // 返回服务器上的时间
    [AjaxPro.AjaxMethod]
    public string GetServerTime()
    {
        System.Threading.Thread.Sleep(6000);
        return DateTime.Now.ToLongTimeString();
    }
    [AjaxPro.AjaxMethod]
    public void Receive(string message)
    {
        System.Collections.Generic.List<string> list
                = HttpContext.Current.Application["Reply"] as System.Collections.Generic.List<string>;
        list.Add(message);
    }
    [AjaxPro.AjaxMethod]
    public string[] GetAllReply()
    {
        System.Collections.Generic.List<string> list
                = HttpContext.Current.Application["Reply"] as System.Collections.Generic.List<string>;
        return list.ToArray();
    }
    [AjaxPro.AjaxMethod]
    public SelectItem[] GetItems(string id)
    {
        // 生成模拟的数据
        SelectItem[] items = new SelectItem[8];
        for (int i = 0; i < items.Length; i++)
        {
            items[i] = new SelectItem();
            items[i].Value = string.Format("{0}00{1}", id, i);
            items[i].Text = string.Format("项目 - {0}", i);
        }
        return items;
    }
}
放一个Global文件:
<%@ Application Language="C#" %>
<script runat="server">
    void Application_Start(object sender, EventArgs e) 
    {
        // 在应用程序启动时运行的代码
        System.Collections.Generic.List<string> list
            = new System.Collections.Generic.List<string>();
HttpContext.Current.Application["Reply"] = list;
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  在应用程序关闭时运行的代码
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // 在出现未处理的错误时运行的代码
}
    void Session_Start(object sender, EventArgs e) 
    {
        // 在新会话启动时运行的代码
}
    void Session_End(object sender, EventArgs e) 
    {
        // 在会话结束时运行的代码。 
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
        // 或 SQLServer,则不会引发该事件。
    }
       
</script>
页面,要引用jQuery.min.js文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" value="获取服务器上的时间" onclick="getServerTime();" />
    </div>
    </form>
    
    <script type="text/javascript">
    
        function getServerTime()
        {
            /*
            // 同步方式
            var result = ServerService.GetServerTime();
            alert( result.value );
            */
            
            // 异步方式
            ServerService.GetServerTime(
                function( result )
                {
                    alert( result.value );
                }
            );
            
        }
    
    </script>
    
</body>
</html>
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号