無(wú)為

          無(wú)為則可為,無(wú)為則至深!

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks
          AJAX技術(shù)所提倡的無(wú)刷新回調(diào),在原來(lái)的技術(shù)中需要寫大量的JavaScript代碼或使用一些AJAX框架,使得開(kāi)發(fā)效率和可維護(hù)性大大降低。其實(shí)ASP.NET2.0中,已經(jīng)提供了這樣的接口,這就是ICallbackEventHandler。
          ??? 關(guān)于ICallbackEventHandler網(wǎng)上已經(jīng)有很多文章介紹了,這篇實(shí)為畫蛇添足。

          ICallbackEventHandler存在于System.Web.UI中,我們先做一個(gè)非常簡(jiǎn)單的例子來(lái)試用一下。

          ?? 第一步,在VS2005中建立一個(gè)新的WEB窗件。
          ?? 第二步,在ASPX中,放上一段HTML代碼(如下):

          1<body>
          2????<form?id="form1"?runat="server">
          3????<div>
          4????????<button?onclick="CallServer()">CallServer</button>
          5????</div>
          6????</form>
          7</body>


          ?? 第三步,然后在<HEAD></HEAD>中放入一段JavaScript腳本:

          ?1?<script?type="text/javascript">
          ?2?????function?CallServer()
          ?3?????{
          ?4?????????var?product?=?"測(cè)試";
          ?5?????????<%=?ClientScript.GetCallbackEventReference(this,?"product",?"ReceiveServerData",null)%>;
          ?6?????}

          ?7?????
          ?8?????function?ReceiveServerData(rValue)
          ?9?????{
          10?????????alert(rValue);
          11?????}

          12?</script>

          ?

          ?? 第四步,在此ASPX的后臺(tái)CS代碼中,繼承ICallbackEventHandler接口,并實(shí)現(xiàn)接口中的兩個(gè)方法:
          ?ICallbackEventHandler.GetCallbackResult()
          ??? 和
          ?ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

          ?? 第五步,增加一個(gè)變量CallBackValue,并修改接口的兩個(gè)方法為:

          ?1?private?string?CallBackValue?=?string.Empty;
          ?2????
          ?3?string?ICallbackEventHandler.GetCallbackResult()
          ?4?{
          ?5??return?CallBackValue?+?",ok";
          ?6?}

          ?7
          ?8?void?ICallbackEventHandler.RaiseCallbackEvent(string?eventArgument)
          ?9?{
          10??this.CallBackValue?=?eventArgument;
          11?}

          12

          ?

          ??? 第六步,運(yùn)行,界面上會(huì)出現(xiàn)一個(gè)按鈕,點(diǎn)擊后,會(huì)將“測(cè)試”這個(gè)字符串傳至后臺(tái),后臺(tái)C#代碼將字符串加上“,OK”后返回給客戶端的JavaScript代碼,并顯示。

          ??? 以上六步,就可以實(shí)現(xiàn)無(wú)刷新回調(diào)了。現(xiàn)在,我們來(lái)分析一下幾段代碼。
          ??? 先看第三步中的JavaScript代碼,其中的CallServer()方法中進(jìn)行了回調(diào),回調(diào)的語(yǔ)句為:
          <%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
          ???
          ??? 里面四個(gè)參數(shù)中第二個(gè)參數(shù)指定將product這個(gè)JavaScript中的字符串變量傳回后臺(tái),第三個(gè)參數(shù)指定了從后臺(tái)返回時(shí)接收返回信息的JavaScript方法ReceiveServerData(string Value)。

          ??? 第五步中后臺(tái)的兩個(gè)方法,一個(gè)ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)用來(lái)接收前臺(tái)JavaScript中傳來(lái)的字符串變量,并賦值給內(nèi)部變量this.CallBackValue,另一個(gè)方法ICallbackEventHandler.GetCallbackResult()將變更后的內(nèi)部變量this.CallBackValue返回給前臺(tái)JavaScript方法ReceiveServerData(string Value)。

          ??? 調(diào)用的順序是: (前臺(tái))CallServer() --> (后臺(tái))ICallbackEventHandler.RaiseCallbackEvent(string eventArgument) --> (后臺(tái))ICallbackEventHandler.GetCallbackResult() --> (前臺(tái))ReceiveServerData(string Value)。

          ??? 整個(gè)調(diào)用過(guò)程非常簡(jiǎn)單,而其中非常關(guān)鍵的一步是第三步的
          <%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
          這個(gè)方法,以下是從網(wǎng)上找來(lái)的一段資料,大家可以看看。

          GetCallbackEventReference使得客戶端方法在客戶端請(qǐng)求結(jié)束時(shí)得到回收。 它也讓CallBackManager 確定產(chǎn)生哪種回叫方法。 在這個(gè)例子內(nèi)使用的被重載的方法是:

          ?? public string GetCallbackEventReference(
          ????? string target, string argument,
          ????? string clientCallback, string? context,
          string clientErrorCallback)
          Table 1. GetCallBackEventReference 方法的參數(shù)描述。
          Parameters Description target ID of the page where the callback invocation is handled. For more see the other overloaded options available in the next immediate section.In our sample "this" is the argument value, since the callback is handled in the same page.? argument This is the parameter defintion used to send value to the server. This value is received by parameter "eventArgument" at the server end using the RaiseCallbackEvent event."arg" becomes the first parameter name in our sample. The value is passed through this argument from the client. clientCallback Method name of the callback that is invoked after successful server call."CallBackHandler" is the method name that handles the callback.?? context A parameter that is associated with the "argument" from the client. It usually should be used to identify the context of the call. You will understand this better from the sample implementation.In the sample "ctx" is just another parameter definition used. The value for this is passed from the client. clientErrorCallback Name of the method that is called from the CallBackManager in case of any errors.
          從這個(gè)方法返回的string是:

          ??
          ?? __doCallback('__Page',arg,CallBackHandler,ctx, ErrorCallBack)
          ?
          另一個(gè)重載方法是:

          ?? public string GetCallbackEventReference(
          ????? Control control, string argument,
          ????? string clientCallback, string? context)
          ??
          ?? public string GetCallbackEventReference(
          ????? Control control, string argument,
          ????? string clientCallback,? string? context,
          string clientErrorCallback)



          凡是有該標(biāo)志的文章,都是該blog博主Caoer(草兒)原創(chuàng),凡是索引、收藏
          、轉(zhuǎn)載請(qǐng)注明來(lái)處和原文作者。非常感謝。

          posted on 2006-06-10 12:49 草兒 閱讀(496) 評(píng)論(0)  編輯  收藏 所屬分類: ajaxDotNet
          主站蜘蛛池模板: 沾益县| 昆山市| 苍山县| 锦屏县| 探索| 甘泉县| 昭平县| 金山区| 彰化市| 龙山县| 吕梁市| 辛集市| 忻城县| 海南省| 东乡族自治县| 大新县| 民和| 太原市| 惠水县| 香港 | 渝北区| 绥化市| 北流市| 额尔古纳市| 雷州市| 道真| 洛扎县| 措勤县| 宁南县| 米林县| 荣昌县| 涟源市| 句容市| 方山县| 遵化市| 成都市| 靖江市| 乐山市| 舞阳县| 乌鲁木齐市| 望城县|