Home > AJAX, ASP.NET, Performance > Performance Programming with ASP.NET Ajax

Performance Programming with ASP.NET Ajax

At first glance, ajax seems to be a cinch with asp.net 3.5, what with all the updatepanel, timers and such. However behind this ease of development hides a danger which most developers may not notice.

Although updatepanels and timers are easy to use, each update triggers a complete postback and if your viewstate is on the page, its an extremely large form post. On the server side, each event of a normal postback is called, and the whole control tree is generated to handle the ajax form post. This is quite intensive both for the server and for the client.

This can be good or bad depending on how you look at it. Its good because it ensures that the postback is a valid one, and also gives you flexibility on the controls you want to change and to include in any javascript that you need, all these being done on the code behind, without the need to fiddle with all the client side javascript required to perform these actions.

On the flip side, there are also client side apis and scriptmanager service references which gives you complete flexibility to go ahead and program using client side javascript.

Nexus initially went with the first approach (i.e a whole bunch of updatepanels), but the results were less than impressive. Moving onto the second approach required a revamp of the whole source code, but this gave the user a much better experience along with substantially reducing server load.

To reduce the footprint on the server side, i utilized ScriptManager’s ServiceReference parameters

<asp:ScriptManager ID="ScriptManager" runat="server"
    <asp:ServiceReference InlineScript="true" Path="JSON.asmx" />
    <asp:ServiceReference InlineScript="true" Path="ProxyAsync.asmx" />

ServiceReferences exposes the webservice functions in client side javascript so you can call these webservices without having to write complicated javascript. for example, i have a webservice function called UserFirstLoad(). This function is called the everytime the user logs into Nexus.

[ScriptMethod(UseHttpGet = false, XmlSerializeString = true)]
public JSON_Tabs UserFirstLoad()

Instead of writing javascript to interface to this webservice, ScriptManager automatically writes it for you and exposes it nicely as JSON.UserFirstLoad(), a function which i can easily call and have the capability to do error handling if need be.

As i’m not using UpdatePanel, GUI updates are done directly via DOM. Fortunately Ajax ClientScripts provide some global functions which i can use to

  1. Locate the element i want to update ($get)
  2. Add events to the element ($addHandler)

It is also fortunate that Visual Studio 2008 comes with javascript intellisense, which autocompletes your javascript as you type.

As Javascript is OOP, i decided to build my client script scripts around objects and functions. This gives me the flexibility to overwrite functions and have nice clean code which i can then easily debug with VS2008.

Categories: AJAX, ASP.NET, Performance
  1. No comments yet.
  1. No trackbacks yet.

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: