ASP.NET 2.0 Client Callbacks, Makes Remote Scripting Easier

xiaoxiao2021-03-06  83

During November Last Year, I Wrote An Article About Clinet Callbaks with ASP.NET 2.0. I Have Now Updated this Article for the May CTP Version of VS 2005), SO Here Is HE Updated Version:

How many of you have used technologies to call server-side code from a client-side code? I think a lot of you have tried some of the technologies that has bean available in the past few years for making client-side code to call server . -side code If we take a few steps back in time, there have been different kinds of technologies of making remote calls from a client to a server I will not mention all of them;. instead I will focus on one most common used technology . I think all of you have heard about Remote Scripting. With Remote Scripting we were able to make client-side calls from a page to server-side script located in an Active Server Pages (ASP) without having to submit the page. The Remote Scripting technology uses a Java Applets to make the communications to the server. The applet communicates with the server over the HTTP protocol and wrapped the data in some sort of XML. to use Remote Scripting we needed two files, one that handles the client side request (RS.htm) and one for firing the code on the server side (rs.asp). With ASP.NET 2.0 "codename Whidbey" there is a new technology for making remote call possible. This new technology is called Client callbacks. With Client callbacks we can make client-side script to call server-side events without causing a postback. This will be a perfect solution for situations like executing server-side code without refreshing the page, preloading of data and validations etc. This new callbacks technology is very similar to Remote Scripting, but INSTEAD OF USING A Java Applet Client Callbacks Uses Xmlhttp to Communicate with The Server, And Instead of The Rs.htm and Rs.asp File ASP.NET 2.0 Uses The A Script-Library Named CallbackManager.

Note To use Client callbacks, XmlHTTP must be supported by the browsers. There is a new properties added to the HttpBrowserCapabilities class that could be used to check if the browser support Client callbacks-SupportsCallBack.How does the Client call backs work

The client makes the call of a client-side method that will use the CallBackManager. The CallBackManager creates the request to an .aspx page on the server. The server processes the page and its events and makes the call of the sever-side function. The CallBackManager then parses the server response and make the call to a callback method located on the client-side. The CallBackManager must know the name of the client-side callback method. The CallbackManager must know which method it should call on the client-side ........................ ..

Let's see how callbacks can be implemented. The following example that I'm going to describe will use callbacks to validate if a number between 1 and 1000 is entered into a textbox. The code for this is shown in Code Sample 1.

Code Sample 1. Default.aspx

<% @ page language = "c #"%> <% @Mplements interface = "system.Web.ui.ICALLBACKEVENTHANDLER"%>