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這一步。相當于走小門回來的,誰都不知道。。
轉載于:https://www.cnblogs.com/chica/archive/2010/11/02/1867203.html
總結
以上是生活随笔為你收集整理的Ajax step by step的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 博客园博客PDF生成器
- 下一篇: centos6.8安装docker,ko