那里有专门做印刷品的网站,电商平台有哪些平台,软件开发招标网站,服装公司做哪个网站ICallbackEventHandler存在于System.Web.UI中#xff0c;我们先做一个非常简单的例子来试用一下。 第一步#xff0c;在VS2005中建立一个新的WEB窗件。 第二步#xff0c;在ASPX中#xff0c;放上一段HTML代码(如下)#xff1a; 1body2form idform1我们先做一个非常简单的例子来试用一下。 第一步在VS2005中建立一个新的WEB窗件。 第二步在ASPX中放上一段HTML代码(如下) 1body2 form idform1 runatserver3 div4 button onclickCallServer()CallServer/button5 /div6 /form7/body 第三步然后在HEAD/HEAD中放入一段JavaScript脚本 1 script typetext/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 3string ICallbackEventHandler.GetCallbackResult() 4{ 5 return CallBackValue ,ok; 6 } 7 8void 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)转载于:https://www.cnblogs.com/n666/archive/2009/09/11/2191139.html