Implementing the callback technique requires the following steps:
1. Implement Icallbakeventhandler
2. Implement the method in the interface: RaiseCallbackEvent
3. Implement GetCallbackResult
Method |
Explain |
Parameters |
void RaiseCallbackEvent (String eventargument) |
Handling callback events for control purposes |
Represents the event arguments to pass to the event handler |
String GetCallbackResult () |
Returns the result of a callback event for the control's purpose |
|
Let's take a look at an example and then explain:
1. Create an ASPX page that reads as follows:
Page code:
Here, you do not see the CallServer function defined, and there is no place to call success and error. and look backstage.
Implements the ICallbackEventHandler interface.
Background code:
protected void Page_Load (object sender, EventArgs e) { String reference = Page.ClientScript.GetCallbackEventReference (This, "args", "Success", "", "Error", false); String callbackscript = "function CallServer (args,context) {\ n" + reference + "; \ n}"; Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "callback", Callbackscript, True); } public string GetCallbackResult () { return viewstate["result"]. ToString (); } public void RaiseCallbackEvent (string eventargument) { if (eventargument.equals ("admin")) { viewstate["Result" = "cannot be used!"; } else { viewstate["result"] = "can be used"; } }
At this point, we see the use of Page.ClientScript.GetCallbackEventReference, as well as the definition of the CallServer method. and see the definition of the GetCallbackEventReference method:
GetCallbackEventReference: Gets a reference to the client parameter, and when called, initiates a callback to the server event. That is: Returns a function that can submit a page to the server.
The function parameters used in the example are interpreted as follows:
GetCallbackEventReference (Control control,string argument,string clientcallback,string context,string Clienterrorcallback,bool UseAsync)
Control: The server control that handles client callbacks.
Argument: A parameter that is passed from the client script to the server's RaiseCallbackEvent event.
Clientcallback: A client function to handle a function when the event runs successfully
Context: Before the callback is started, client script is evaluated on the client, and the result of the script is passed to the event handler.
clientErrorCallback: A client function that handles functions that run when an event fails. (although this parameter is present in this example, it is not actually used)
Useasync:true indicates asynchronous execution of callbacks, false means synchronous execution callback
Run the page, see the effect, whether the page is not refreshed when the user is judged.
The page execution process is as follows:
1. Call the CallServer method when the textbox loses focus
The 2.CallServer method passes the This.value value into the parameters of the RaiseCallbackEvent method, then makes a judgement, and then calls the GetCallbackResult () method
3. At this point, execute the Success method defined in the page.
To view the source code of the running page, the resulting client HTML code is as follows:
Note This code in the page:
<script type= "Text/javascript" >//<! [Cdata[function callserver (Args,context) {webform_docallback (' __page ', args,success, "", Error,false);} ]]></script>
This code is generated in the Pageload event.
protected void Page_Load (object sender, EventArgs e) { String reference = Page.ClientScript.GetCallbackEventReference (This, "args", "Success", "", "Error", false); String callbackscript = "function CallServer (args,context) {\ n" + reference + "; \ n}"; Page.ClientScript.RegisterClientScriptBlock (this. GetType (), "callback", Callbackscript, True); }
At this point, look at the Webform_docallback method.
Put down a script code in the source code./kongjian/webresource.axd?d=kyfh-s4vutwcm_01ankeqq2&t=633934581566718750
Find the Webform_docallback method code in it and simply list the parts:
var xmlrequest,e; try { xmlrequest = new XMLHttpRequest (); } catch (E) { try { xmlrequest = new ActiveXObject ("Microsoft.XMLHTTP"); } catch (E) { } } var setrequestheadermethodexists = true; try { setrequestheadermethodexists = (xmlrequest && xmlrequest.setrequestheader); } catch (e) {} var callback = new Object (); Callback.eventcallback = Eventcallback; Callback.context = context; Callback.errorcallback = Errorcallback; Callback.async = UseAsync;
See this code, you will find that the original has been established XMLHttpRequest object. I won't say it back.
asp.net--Callback Technology