ASP.NET - what is callback in asp.net - Asked By sri on 15-Oct-11 02:17 AM

what is callback in asp.net
dipa ahuja replied to sri on 15-Oct-11 02:33 AM
Untitled document
Callback : It is a way to send a request to the web page from the client script.
 
In callback a client function sends a request and a special marked method is invoked on the server. It does the processing & returns the value which is received by another client function to process the result.
 
To have client side callbacks, the page has to implement ICallbackEventHandler & implement functions RaiseCallBackEvent & GetCallBackResult.
Suchit shah replied to sri on 15-Oct-11 03:02 AM

In the default model for ASP.NET Web pages, the user interacts with a page and clicks a button or performs some other action that results in a postback. The page and its controls are re-created, the page code runs on the server, and a new version of the page is rendered to the browser. However, in some situations, it is useful to run server code from the client without performing a postback. If the client script in the page is maintaining some state information (for example, local variable values), posting the page and getting a new copy of it destroys that state. Additionally, page postbacks introduce processing overhead that can decrease performance and force the user to wait for the page to be processed and re-created.

To avoid losing client state and not incur the processing overhead of a server roundtrip, you can code an ASP.NET Web page so that it can perform client callbacks. In a client callback, a client-script function sends a request to an ASP.NET Web page. The Web page runs a modified version of its normal life cycle. The page is initiated and its controls and other members are created, and then a specially marked method is invoked. The method performs the processing that you have coded and then returns a value to the browser that can be read by another client script function. Throughout this process, the page is live in the browser.

Several Web server controls can use client callbacks. For example, the http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.aspx control uses client callbacks to implement its populate-on-demand functionality. For more information see http://msdn.microsoft.com/en-us/library/e8z5184w.aspx.

There are several options for automating client callbacks in an ASP.NET Web page. AJAX features in ASP.NET such as the http://msdn.microsoft.com/en-us/library/system.web.ui.updatepanel.aspx server control can automate asynchronous partial-page updates for you, and the Web service communication feature can automate asynchronous Web service calls.


Creating an ASP.NET page that programmatically implements client callbacks is similar to creating any ASP.NET page, with a few these differences. The page's server code must perform the following tasks:

  • Implement the http://msdn.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler.aspx interface. You can add this interface declaration to any ASP.NET Web page.

  • Provide an implementation for the http://msdn.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler.raisecallbackevent.aspx method. This method will be invoked to perform the callback on the server.

  • Provide an implementation for the http://msdn.microsoft.com/en-us/library/system.web.ui.icallbackeventhandler.getcallbackresult.aspx method. This method will return the callback result to the client.

In addition, the page must contain three client script functions that perform the following actions:

  • One function calls a helper method that performs the actual request to the server. In this function, you can perform custom logic to prepare the event arguments first. You can send a string as a parameter to the server-side callback event handler.

  • Another function receives (is called by) the result from the server code that processed the callback event, accepting a string that represents the results. This is referred to as the client callback function.

  • A third function is the helper function that performs the actual request to the server. This function is generated automatically by ASP.NET when you generate a reference to this function by using the http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getcallbackeventreference.aspx method in server code.

Both client callbacks and postbacks are requests for the originating page. Therefore, client callbacks and postbacks are recorded in Web server logs as a page request.



In a client callback, a client script function sends a request to the ASP.NET Web page, which then runs an abbreviated version of its normal life cycle to process the callback. To ensure that callback events originate from the expected user interface (UI), you can validate callbacks. In callback validation, you register an event for validation during the Web page rendering and then validate the event during the callback. For an overview of callbacks, see http://msdn.microsoft.com/en-us/library/ms178208.aspx.

Its important to note that Client Callback uses XmlHttp, which, as far as I know, only works in IE. Hopefully workarounds and third party solutions aren't far away, which'll make this a truly powerful weapon to have in our arsenal.

http://www.codeproject.com/KB/aspnet/ClientCallBackAspNet2.aspx
http://msdn.microsoft.com/en-us/library/ms366518.aspx
Riley K replied to sri on 15-Oct-11 03:48 AM

   CallBacks are used to improve client experience as they help in avoiding whole page refresh

So using callbacks, we can get the data from the server, and the end user will not be able to feel it  . Moreover, we can also perform server side operations like saving the passed info into a database and then sending a fresh string back to the client which can present the client with an updated view.

The ASP.NET 2.0, when it started coming up with the callback, it started with a new interface, ICallbackEventHandler

You can refer this link

http://www.simple-talk.com/dotnet/asp.net/asynchronous-client-script-callbacks/
http://www.codeproject.com/KB/aspnet/clientcallback.aspx#2

Regards





Reena Jain replied to sri on 15-Oct-11 06:46 AM
Hi,

ASP.NET 2.0 includes a new Client Callback feature that enables you to retrieve page values and populate them to an already-generated page with out reconstructing page. This makes it possible to use on a page with out going through the entire post back cycle; that means you can update your pages without completely redrawing the page.

A callback is executable code that is passed as an argument to other code. It allows a lower-level software layer to call a subroutine (or function) defined in a higher-level layer.