ASP.Net ICallback Vijayalakshmi G M Senior Trainer Binary Spectrum
Topics ASP.Net – Client Callback ICallback Implementation ICallback Example
ASP.Net ICallback - Introduction
ASP.Net – Postback V/S Callback Postback –Recreates the web page and controls for Entire page and returns the Entire script to the client. –Works based on Synchronized processing model [User should wait till the page is refreshed]. –Huge data gets transferred from Client to Server or vice-versa. Callback –Recreates only the part of the page and control and respond to the client. –Works based on Asynchronous processing model [User can continue with another job]. –Less data gets transferred from client and server.
ASP.Net – Client Callback Feature provided by ASP.Net Used to avoid full post back in the asp.net page. Good Alternative to Ajax Update Panel. Definition “Client Callback provides us a way to call a server side code/method asynchronously and fetch some new data without page refresh”
Callback process Flow
ASP.Net – Client Callback 1.Client Callback is initiated from JavaScript from client browser. 2.Result will be displayed on the client browser after the required modification from Server.
How to implement Client Callback Server side: –Implement interface ICallbackEventHandler on any page or control where implemented. –Need to implement two methods RaiseCallbackEvent and GetCallbackResult provided by the interface ICallbackEventHandler. –RaiseCallbackEvent event is called to perform the Callback on server. –GetCallbackResult event returns the result of the callback.
How to implement Client Callback Client side: –Javascript function registered with the Server to initiate a client call. –It takes one parameter to pass the value to the server. –Another JavaScript function to return the callback result. –Helper function which makes actual request to the server [automatically generated by ASP.NET when you generate a reference to this function by using the GetCallbackEventReference method in server code.]
ICallback - Implementation
Steps : To Implement Callbacks 1.Use ICallbackEventHandler[ System.Web.UI Namespace] Interface. Implement this interface on Page or a Control.
Steps : To Implement Callbacks 2. Server side function public void RaiseCallbackEvent(String eventArgument) – Automatically called whenever there is a callback.
Steps : To Implement Callbacks 2. Server side function public String GetCallbackResult() – returns the output to the client.
Steps : To Implement Callbacks 2. GetCallbackEventReference() – Register Client side script at Page load. we need to create a Callback reference, that is the Client side method that is called, after finishing the callback, and assign that reference in the method that initiates callback from Client.
GetCallbackEventReference() - Method Control - The server Control that handles the client callback.Control Argument - An argument passed from the client script to the server. clientCallback - The name of the client event handler that receives the result of the successful server event. Context - Client script that is evaluated on the client prior to initiating the callback.
GetCallbackEventReference() - Method
Steps : To Implement Callbacks 3. Client side function Javascript function – to raise a client callback. event.
Steps : To Implement Callbacks 3. Client side function Javascript function – which will handle the response from the server. This function will be called automatically after GetCallbackResult().
Steps : To Implement Callbacks 4. HTML Form
Client Callback v/s Postback
Callback – When and Where to use Light Action and gives better performance over update panel. It should be used only for display purpose.
Callback – Drawbacks Cross browser support –Callbacks are supported by Internet Explorer, Firefox and other Mozilla-compliant browsers. Not all the browsers support partial page updation. Script code should strictly follow the W3 DOM standards Server's Response Delivery Format –Server responds with String data, works well with small amount of data. [becomes problematic with large complex data]. Problem with multiple callbacks
Resource
Icallback - Resources Exploring-Client-Callback.aspx?display=Mobilehttp:// Exploring-Client-Callback.aspx?display=Mobile sing-icallbackeventhandler-in-aspnet/ sing-icallbackeventhandler-in-aspnet/ menting-icallbackeventhandler-in.htmlhttp:// menting-icallbackeventhandler-in.html
Thank you