Ajax step by step

ArcGIS Web ADF开发中较多的使用了GetCallbackEventReference方法,由于本人是AJAX菜鸟,本着从小李子(例子)开始的精神,找到了如下一个例子:

(代码源自MIDI大侠的博客:http://www.cnblogs.com/yinhaiming/articles/1486100.html

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test_ajax.aspx.cs" Inherits="test_ajax" %>

<!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>
    <script type="text/javascript">
    function onclickfun(a){
    document.getElementById("tb1").value=a;    
    }
    
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="tb1" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class test_ajax : System.Web.UI.Page, ICallbackEventHandler
{
    private string callbackResult = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsCallback)
        { 
        string callbackMethod = this.Page.ClientScript.GetCallbackEventReference(this, 
            "document.getElementById('tb1').value", "onclickfun", "this.value");
            tb1.Attributes.Add("OnClick", callbackMethod);
        }
    }
    public string GetCallbackResult() 
    {
        return callbackResult;

    }
    public void RaiseCallbackEvent(string eventArgument)
    {

        callbackResult = eventArgument + DateTime.Now.ToString();
    }
}

运行后,页面上出现一个文本框,点击文本框,文本框中显示当前日期,再点击,当前日期续在上一个日期之后。分析运行过程:(1)鼠标点击文本框,这个操作被callbackMethod所捕获,因为tb1.Attributes.Add("OnClick", callbackMethod); 一句建立了两者之间的关系;(2)然后document.getElementById('tb1').value通过eventArgument被传递给RaiseCallbackEvent函数,获取服务器当前日期;(3)接下来是GetCallbackResult,传递callbackResult给onclickfun,数据重新回到客户端(4)onclickfun更新文本框的值那么ajax体现在哪里呢,按照postback的机制,客户端点击,服务端响应日期,客户端接收(同时伴随着刷新)。(局部刷新只是表象,本质特征是数据无刷新传递)所以关键是传递数值给onclickfun这一步。相当于走小门回来的,谁都不知道。。

posted @ 2010-11-02 15:27  chica  阅读(164)  评论(0编辑  收藏  举报