Objects on the client with ASP.NET AJAX

In this post i would like to show you how cool it is to use the objects you defined in your Business Layer on the client. But first why do you want to use your objects on the client? Well, wouldn’t it be nice to only retrieve some objects from the server once and manipulate the objects, add new objects and when everything is good then send it back to the server to process everything.  This means less data transfers. With the ASP.NET AJAX framework this is very easy to achieve. I have a webpage which communicates with a webservice to retrieve some information from the server. My webservice only contains two webmethods. A getHouse and a saveHouse Webmethod.


public House GetHouse()


return new House();


In this case I created a Class House which will be send to the client when the webmethod GetHouse is executed. As you can see below the house object in the Business Namespace is available on the client.GetHouse

The reason this object is available on the client is because I have set a reference to the webservice in the Scriptmanager. The Scriptmanager will create a Javascript proxy to enable communication with the  webservice. Because I have defined a webmethod where I use an object House this object will be generated in the Javascript proxy as show in de second screenshot.


Here you can see that first the namespace is created and later the House object will be registered in the Business namespace. Because the object is generated in the Javascript Proxy it becomes available on the client. Now we can write code like:

var house = new Business.House();

But what if I want to use an object which I don’t use in my webmethodes. For example I would like to add a Room to the House object. The webservice doesn’t have a webmethod where it uses a Room object, so the JavascriptProxy will not contain a Room object. I have to make sure that the object from my Business Layer will be available through the Javascrit proxy. This can be achieved by using the [GenerateScriptType(typeof(Room))] Attribute. With this attribute you specify that the server type must be generated into the Javascript Proxy. So let’s take a look at the proxy again now i have added this attribute to my Webservice Class.GenerateMultipleClasses

Right now I have the Room and House class available on the client. See the final screenshot

House with Room

Using objects on your client can reduce data tranfsers. You can create the objects on the client and when you are happy with it send it back to the server where it will be processed. I think this is a pretty cool and strong feature of the Javascript proxy which is mainly responsible for this feature. 


One Response to Objects on the client with ASP.NET AJAX

  1. Alexwebmaster says:

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: