Making Ajax Requests with AjaxMethodCallback

Home Home | Reset Page Reset Page | Page created: 5/24/2012 5:08:47 AM
This page shows a few simple function examples how to use the AjaxMethodCallback control to make Ajax callbacks to the same page by using [CallbackMethod] attribute on Page methods. Messages are passed using JSON, but effectively you can server methods directly either on the same page (as in this example) or in a separte Callback HttpHandler for better efficiency. This example also uses optionally the ScriptVariables component to normalize ClientIds, and the ScriptContainer control to host scripts on the page.

Hello World Callback

Most basic callback that simply echoes back a string without a Postback
Please enter your name:

Add Two Values

Another simple example that adds two values by passing numeric values between client and server. Server method simply accepts and returns numeric values which are marshalled back to the client.
Please enter two numbers to add: +

Return Complex Objects

You can also easily pass complex objects to and from the server. This example retrieves a stock quote and displays some simple data. To see data on the wire you can use FireBug (FireFox) or Fiddler (generic)
Please enter a stock symbol:

Return Lists of Objects

Lists can also be returned easily. Most common list types serialize into JSON

Load an object and then save it

This object is loaded when the list above is updated. You can then change values and save to update the data on the server.
Last Name:
First Name:
Company:
Bill Rate:

Errors and Timeout Results

Callbacks that fail will bring back exceptions as JSON objects and they are automatically routed the the error handler. The handler provides an object regardless of the type of failure.
Throw an exception | Cause a Timeout