日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

使用ICallbackEventHandler实现无刷新回调

發(fā)布時(shí)間:2023/12/16 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用ICallbackEventHandler实现无刷新回调 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

AJAX技術(shù)所提倡的無刷新回調(diào),在原來的技術(shù)中需要寫大量的JavaScript代碼或使用一些AJAX框架,使得開發(fā)效率和可維護(hù)性大大降低。其實(shí)ASP.NET2.0中,已經(jīng)提供了這樣的接口,這就是ICallbackEventHandler。
???? 關(guān)于ICallbackEventHandler網(wǎng)上已經(jīng)有很多文章介紹了,這篇實(shí)為畫蛇添足。

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

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

<body>
<form id="form1" runat="server">
<div>
<button οnclick="CallServer()">CallServer</button>
</div>
</form>
</body> <body> <form id="form1" runat="server"> <div> <button οnclick="CallServer()">CallServer</button> </div> </form> </body>

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

<script type="text/javascript">
function CallServer()
{
var product = "測試";
<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
}
function ReceiveServerData(rValue)
{
alert(rValue);
}
</script>


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

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

private string CallBackValue = string.Empty;
string ICallbackEventHandler.GetCallbackResult()
{
return CallBackValue + ",ok";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
this.CallBackValue = eventArgument;
}

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

???? 以上六步,就可以實(shí)現(xiàn)無刷新回調(diào)了。現(xiàn)在,我們來分析一下幾段代碼。
???? 先看第三步中的JavaScript代碼,其中的CallServer()方法中進(jìn)行了回調(diào),回調(diào)的語句為:

<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;

里面四個(gè)參數(shù)中第二個(gè)參數(shù)指定將product這個(gè)JavaScript中的字符串變量傳回后臺,第三個(gè)參數(shù)指定了從后臺返回時(shí)接收返回信息的JavaScript方法ReceiveServerData(string Value)。

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

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

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

GetCallbackEventReference使得客戶端方法在客戶端請求結(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)
鏈接:
http://www.cnblogs.com/vcool/archive/2008/02/27/1083683.html

轉(zhuǎn)載于:https://www.cnblogs.com/CodingPerfectWorld/archive/2010/11/23/1885133.html

總結(jié)

以上是生活随笔為你收集整理的使用ICallbackEventHandler实现无刷新回调的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。