Hello World


Tuesday, December 18, 2007

AJAX .Net Wrapper

Asynchronous JavaScript and XML (AJAX)


Ajax: A New Approach to Web Applications


Download the Ajax DLL from:
Ajax.NET
Add the Ajax.dll reference to your application.
Add the using Ajax; namespace to your page.
Add the httpHandler to your web.config.







Sample Code to try.

Ajax can return custom classes. All that is required is that the class be marked with the Serializable attribute. Given the following class:

[Serializable()]
public class User
{
private int _userId;
private string _firstName;
private string _lastName;
public int userId{ get { return _userId; } }
public string FirstName{ get { return _firstName; } }
public string LastName{ get { return _lastName; } }

public User(int _userId, string _firstName, string _lastName)
{
this._userId = _userId;
this._firstName = _firstName;
this._lastName = _lastName;
}
public User(){}

[AjaxMethod()]
public static User GetUser(int userId)
{
// The userId would normally be a value to use in a DB call.
return new User(userId,"Elvis", "Costello");
}

We would register the GetUser proxy via a call to the RegisterTypeForAjax
private void Page_Load(object sender, EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(User), this.Page);
}

Allowing us to asynchronously call the GetUser in client-side code with code such as:

















The value returned in the response is actually an object which exposes the same properties as a server-side object (FirstName, LastName and UserId).

No comments:

4GuysFromRolla.com Headlines