Library tutorials & articles
Implementing AJAX in ASP.NET
- Background
- Getting Started
- Setting up the Page
- Dealing with Types
- Miscellaneous
Dealing with Types
Returning complex types
The Ajax wrapper is capable of handling more than just the integer our ServerSideAdd function returned. It currently supports integers, strings, double, booleans, DateTime, DataSets and DataTables, as well as the primitive types of custom classes and arrays. All other types have their ToString values returned.
Returned DataSets work much like real .NET DataSet. Given a server side function which returns a DataSet, we could display the contents client side via:
<script language="JavaScript">
//Asynchronous call to the mythical "GetDataSet" server-side function
function getDataSet(){
AjaxFunctions.GetDataSet(GetDataSet_callback);
}
function GetDataSet_callback(response){
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null){
var s = new Array();
s[s.length] = "<table border=1>";
for(var i=0; i<ds.Tables[0].Rows.length; i++){
s[s.length] = "<tr>";
s[s.length] = "<td>" + ds.Tables[0].Rows[i].FirstName + "</td>";
s[s.length] = "<td>" + ds.Tables[0].Rows[i].Birthday + "</td>";
s[s.length] = "</tr>";
}
s[s.length] = "</table>";
tableDisplay.innerHTML = s.join("");
}
else {
alert("Error. [3001] " + response.request.responseText);
}
}
</script>
Ajax can also 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){
//Replace this with a DB hit or something :)
return new User(userId,"Michael", "Schwarz");
}
}
We would register the GetUser proxy via a call to the RegisterTypeForAjax:
private void Page_Load(object sender, EventArgs e){
Utility.RegisterTypeForAjax(typeof(User));
}
Allowing us to asynchronously call the GetUser in client-side code with code such as:
<script language="javascript">
function getUser(userId){
User.GetUser(GetUser_callback);
}
function GetUser_callback(response){
if (response != null && response.value != null){
var user = response.value;
if (typeof(user) == "object"){
alert(user.FirstName + " " + user.LastName);
}
}
}
getUser(1);
</script>
The value returned in the response is actually an object which exposes the same properties as a server-side object (FirstName, LastName and UserId).
Custom Converters
As we’ve seen, the Ajax .NET wrapper is able to deal with many different .NET types. However, aside from a handful of .NET classes and the built-in types, the wrapper simply calls ToString() on anything it can’t properly return. To get around this, the Ajax .NET wrapper allows developers to create object converters which can be used to smoothly communicate complex objects between the server and the client.
This guide will be updated with additional information on custom converters shortly (sorry).
Related articles
Related discussion
-
Enterprise application - DHTML or Silverlight???
by stephanc (3 replies)
-
How to debug classic ASP pages during AJAX calls in ASP.NET website
by andwan0 (0 replies)
-
Drag and drop controls at runtime in ASP.NET
by srikanthakshay (0 replies)
-
how to send javascript enable html page to email using asp.net
by shahid123 (0 replies)
-
AJAX Made Simple - jQuery, jQuery UI, ASP.NET
by tkruvgt (0 replies)
Related podcasts
-
Brad Abrams: .NET Yesterday & Today
Brad Abrams, Group Program Manager for UI Framework and Services, sits down with Craig Shoemaker to discuss some of the origins of the .NET Framework and what's to come from Silverlight, ASP.NET MVC and ASP.NET AJAX.
Events coming up
-
Apr
17
WebTech Conference 2010 - Bulgaria
Veliko Turnovo, Bulgaria
6th edition of WebTech conference will be held. A 2 day conference about : - Web Technologies - Blogs and blogging - Web 3.0 - Open Web - Mobile technologies - Internet Business
Comments
Leave a comment
Sign in or Join us (it's free).