In asp.net 2 making a client callback is a 4 step process:
1. First of all you need to retrieve some data from the client side, which is accessed by using DOM and JavaScript.
2. The data accessed in step 1 will be sent to the server side function which will process the data, for that you need a client side function which will call that server side method asynchronously.
3. The server method called asynchronously in step 2 will now process the data and then return the some result back to the client.
4. The result produced in step 3 by the server and sent to the client will be used to update the client by using DOM and JavaScript.
Implementing Client Callback functionality on the Server
To implement the client callback on the server side your class must implement the ICallbackEventHandler interface. In our case the Page class is the class which will implement this interface. This interface is used to indicate that a control can be the target of callback event on the server, i.e. a client can call its methods. The interface implements two methods RaiseCallbackEvent and GetCallbackResult.
RaiseCallbackEvent method takes one string argument. This is the method actually called by the client side function and passes the username text box value in the string argument. Once this method produces result then GetCallbackResult function is invoked and returns the string produced by RaiseCallbackEvent. GetCallbackResult does not take any argument, and RaiseCallbackEvent returns void.
Calling the Server Method
Now we need a function which can call the server method asynchronously. We use the GetCallbackEventReference method of the ClientScript property of the Page class in Asp.net 2, and it generates the code to call the server method.
The GetCallbackEventReference method takes five arguments as follows:
1. The reference of the control which has implented the ICallbackEventHandler interface. You do this by providing ‘this’ keyword in the first parameter. Now the GetCallbackEventReference know that the current class will have a method RaiseCallbackEvent which needs to be invoked.
2. The string value that the client must pass to the server-side method (RaiseCallbackEvent). This will be the value of the username textbox which we have extracted using lookup Value () client side function.
3. The name of the JavaScript function (displayResult) that will be automatically called when the response from the server arrives. When the GetCallbackResult method returns value that value is catches by displayResult client side function’s parameter (rValue).
4. The string value that is passed to the dispalyResult function. (This parameter will not be used in this tutorial series.)
5. A boolean value that specifies whether the callback should be done asynchronously. In our case it will be true. Controls like GridView uses synchronous Callbacks.
So how this all is going to work?
GetCallbackEventReference returns a string, which actually will be the content of the function which calls the server side method (RaiseCallbackEvent). The function which calls the server side method will be created in the Load event of the page using RegisterClientScriptBlock method of the ClientScript property of the page.
The names of the arguments that are accepted by the function you are generating ust match the names of the values that you pass to the GetCallbackEventReference method.
Note: Read the following steps slowly and review your code after each step.
1. The lookupValue javascript function gets the value from the textbox. Written by you in the script section.
2. The lookupValue function then passes the value of the textbox to the CallServer function. The callserver function is created from the Page_Load event and GetCallbackEventReference was passed as its content.
3. The CallServer function invokes the RaiseCallbackEvent method of the page, with the help of GetCallbackEventReference. The RaiseCallbackEvent takes a parameter arg in CallServer and eventArgument in RaiseCallbackEvent. RaiseCallbackEvent checks for the user and stores a string result in a variable.
4. The result is returned to the client from the GetCallbackResult method, which is invoked by GetCallbackEventReference.
5. The GetCallbackEventReference then passes the result to the displayResult function on the client side, and that function sets the inner html of the result span to the result sent by GetCallbackEventReference.
source:http://www.themastech.net/Tutorials/ClientCallbacks/Default.aspx
Filed under: RaiseCallBack, asp.net controls gallery, callback, client