asp.net 2.0 Callback enables asp.net server-side controls to invoke the service-side approach without performing a complete postback process, which means that the server-side controls can support Ajax well.
Callback differs from postback in that when a request is sent to the server only ViewState and user-defined information is sent to the server side, and after the request is completed, only the user-defined results are returned, not from the render page.
The GridView control uses this technology to achieve paging and sorting without refreshing, so how does it work, and how do we add that functionality to our custom controls? This article will briefly explain how to use the ASP.net 2.0 callback to implement Ajax.
1, ICallbackEventHandler interface
asp.net server-side controls can accept client callback events by implementing the ICallbackEventHandler interface.
ICallbackEventHandler Interface declaration:
1public interface ICallbackEventHandler
2{
3 string GetCallbackResult ();
4 void RaiseCallbackEvent (string eventargument);
5}
RaiseCallbackEvent handles the client callback event, where the method parameter eventargument is provided by the client script when the callback is triggered, and the method relies on the eventargument parameter for processing.
GetCallbackResult is responsible for returning the processing result as a string to the client script. When the callback completes, the client script will update the page locally based on the resulting processing.
2, Callbackeventreference
So how to register a client script to trigger callback, what else do you need to do?
The ClientScriptManager class is used to manage client script on a Web page, providing a series of methods for registering the script and also obtaining a reference to the specified client script function. We can get a reference to the client function through the GetCallbackEventReference method of the ClientScriptManager class. When the function is invoked on the client, a client callback is initiated.
GetCallbackEventReference Method declaration:
public string GetCallbackEventReference control
,
string argument,
string clientcallback,
String context,
string clienterrorcallback,
bool Useasync
)