<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    無為

    無為則可為,無為則至深!

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      190 Posts :: 291 Stories :: 258 Comments :: 0 Trackbacks
    AJAX技術所提倡的無刷新回調,在原來的技術中需要寫大量的JavaScript代碼或使用一些AJAX框架,使得開發效率和可維護性大大降低。其實ASP.NET2.0中,已經提供了這樣的接口,這就是ICallbackEventHandler。
    ??? 關于ICallbackEventHandler網上已經有很多文章介紹了,這篇實為畫蛇添足。

    ICallbackEventHandler存在于System.Web.UI中,我們先做一個非常簡單的例子來試用一下。

    ?? 第一步,在VS2005中建立一個新的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?=?"測試";
    ?5?????????<%=?ClientScript.GetCallbackEventReference(this,?"product",?"ReceiveServerData",null)%>;
    ?6?????}

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

    12?</script>

    ?

    ?? 第四步,在此ASPX的后臺CS代碼中,繼承ICallbackEventHandler接口,并實現接口中的兩個方法:
    ?ICallbackEventHandler.GetCallbackResult()
    ??? 和
    ?ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

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

    ?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

    ?

    ??? 第六步,運行,界面上會出現一個按鈕,點擊后,會將“測試”這個字符串傳至后臺,后臺C#代碼將字符串加上“,OK”后返回給客戶端的JavaScript代碼,并顯示。

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

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

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

    ??? 整個調用過程非常簡單,而其中非常關鍵的一步是第三步的
    <%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
    這個方法,以下是從網上找來的一段資料,大家可以看看。

    GetCallbackEventReference使得客戶端方法在客戶端請求結束時得到回收。 它也讓CallBackManager 確定產生哪種回叫方法。 在這個例子內使用的被重載的方法是:

    ?? public string GetCallbackEventReference(
    ????? string target, string argument,
    ????? string clientCallback, string? context,
    string clientErrorCallback)
    Table 1. GetCallBackEventReference 方法的參數描述。
    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.
    從這個方法返回的string是:

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

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



    凡是有該標志的文章,都是該blog博主Caoer(草兒)原創,凡是索引、收藏
    、轉載請注明來處和原文作者。非常感謝。

    posted on 2006-06-10 12:49 草兒 閱讀(488) 評論(0)  編輯  收藏 所屬分類: ajaxDotNet
    主站蜘蛛池模板: 亚洲日韩激情无码一区| 德国女人一级毛片免费| 国产成人精品曰本亚洲79ren| mm1313亚洲国产精品无码试看| 免费黄色小视频网站| 亚洲AV无码一区二区一二区| 免费看的成人yellow视频| 亚洲大码熟女在线观看| 免费在线一级毛片| 一级做a爰片久久免费| 亚洲中文字幕不卡无码| 精品国产污污免费网站| 91亚洲va在线天线va天堂va国产 | 亚洲AV无码乱码在线观看代蜜桃| 日韩视频在线精品视频免费观看| 亚洲乱码日产精品BD在线观看| 成人毛片免费观看视频大全| 处破女第一次亚洲18分钟| 精品亚洲成α人无码成α在线观看 | 日本免费人成黄页网观看视频| 精品亚洲成a人在线观看| 亚洲欧洲精品成人久久曰影片 | 亚洲成aⅴ人在线观看| 最新中文字幕免费视频| 九九九精品视频免费| 亚洲av无码一区二区三区乱子伦| 日韩精品内射视频免费观看| 国产精品亚洲综合久久| www国产亚洲精品久久久| 拍拍拍无挡免费视频网站| 91亚洲视频在线观看| 免费一级毛片在线播放不收费| 成在人线av无码免费高潮喷水| 亚洲性无码av在线| 国产午夜无码视频免费网站| 精品视频一区二区三区免费| 亚洲熟妇自偷自拍另欧美| 久久久久久亚洲精品不卡| 国产精品爱啪在线线免费观看| 一级毛片无遮挡免费全部| 亚洲国产精品人久久电影|