Julie Lerman's DevLife

DevLife Part I [May 2005 - March 2007]

My Links

Blog Stats

News

A blog for DevSource.com.

This blog was originally part of the blogs.ziffdavis.com site from May 2005 through June 2007 when the blog was moved to the Movable Type blog engine and hosted at blog.devsource.com/devlife.
The original blog was eventually shut down and I was given the posts so that I could host them on my own site.


Archives

I took the AJAX and ATLAS plunge(s)!

I have been doing a new training session on the Asynchronous programming features in ASP.NET 2.0. The key features of this talk (all new to ASP.NET 2.0) are asynchronous pages, asynchronous tasks, new Async & Completed calls in web service proxies and asynchronous SQL Commands (ExecuteReader, ExecuteXMLReader an ExecuteNonQuery now have Begin/End options). The talk was originally created for the Fall 2006 ASP Connections, but I'm happy to be getting it out there already.

ASP.NET 2.0's Clientside Callbacks

In addition to this, I was showing the ClientSide Callback functionality of ASP.NET 2.0. This was a really exciting feature that tried to make XMLHttp more accessible to coders (and browsers) than before. But it was built before AJAX came to town and is still extremely complex to implement.

There are a lot of steps involved.

On the server side (code behind), you need to do five things:

  1. Have the page implement ICallbackEventHandler
  2. Create a method that gets the new data (implements RaiseCallbackEvent)
  3. Create a method that returns this new data to the client (implements GetCallbackResult)
  4. Wire up the page, these functions and the client script that updates the control using GetCallbackEventReference
  5. Create some script that will get inserted into the html via RegisterClientScriptBlock which will provide the client side wiring back to the server

It took me quite some time to get my head wrapped around all of this in a way that I can now understand it as these steps.

But that's not all!

On the client side you need to

  1. Create the control to be updated
  2. Create script that will update this control when the new data comes back (this is the script referenced in step #4 above)
  3. Create a mechanism to fire all of this off - a button, an event on another control

It's more than a mouthful to try to explain this to a room filled with people of various asp.net and scripting experience.

THIS HAS GOT TO BE EASIER! OH, IT IS!

So, as an experiment, today I installed AJAX.NET and the latest CTP for ATLAS and re-created my demo using these.

My demo is pretty simplistic.

I have an html text box, a drop down list, a button  and a label.

The drop down list shows categories from the AdventureWorks database. I select a category and then click the button. The category id is passed to a function on the server side that returns the sales volume for that category. The label is updated with the sales volume.

The html text box is just there to prove that the page is not posting back. I type some info in it. On full postback, the text box goes back to it's default - empty.

AJAX.NET

Now doing this with AJAX.NET was much simpler. Note that AJAX.NET, an AJAX framework for ASP.NET 1.1 and 2.0, is an open source project by German developer Michael Schwarz. Michael started this as an experiment and it has turned into a heavily used tool by many! He continues to put an enormous amount of effort into it. If you use it in your sites, have a heart and make a contribution (code or cash)!

Here are the steps.

  1. Reference the AJAX.NET's dll
  2. Copy and paste an HttpHandler element into web.config.
  3. Mark the method that returns the new data as an AJAX Web Method by using the AjaxPro.AjaxMethod() attribute. 
  4. Add two pieces of client side script. One is the callback method which will update the label with the incoming data. The other is the "do it" method, which makes the call to the server function and then ties it to the callback method. Since I was passing in an attribute, it took me a few extra minutes to figure out how to implement it properly.

This was amazingly easier than ClientSide Callbacks. I didn't have to do all of that crazy wiring, Steps 1,2,4 and 5 of the client-side callback are eliminated. Step 3 becomes the AjaxPro.AjaxMethod instead of impelementing RaiseCallbackEvent. You still need to know a little bit about scripting, but this was completely painless and it will be really easy to demonstrate.

ATLAS

Next of course was Atlas. I downloaded the latest CTP and just to make sure I knew the absolute getting started basics, watched this short video by Joe Stagner (from the Videos page of the www.asp.net website).

The neat thing about Atlas is that I can just do good ol' draggy droppy development. This is going to be a huge bonus for beginner developers. And I don't have to any scripting. This will also be great for many developers. I have seen the deer-in-the-headlights look when I have used script in demos. Not that it's something for gurus. But your average windows (read moving from VB6 or C++ vs. moving from any of classic web development tools) is definitely daunted by scripting at first.

So all of the coding (for my little sample) is done on the server side.

My "get the new sales data button" is now an asp:button and has a server side click event that updates my label. The label was changed to an asp:label. The special sauce here is that I needed to drag and drop and update panel onto my form and then put the asp:label AND the asp:button inside of the panel. Note that I started with an ATLAS template, so all of the other goo was already in place. I only needed to modify a default property of the script manager (which is already existing on a page from the template) so that it knows I plan to do partial updates.

I played with Atlas enough after PDC to do a few demos of it last fall. But haven't looked at it much since today and boy has it evolved as it will continue to do.

I'M SOLD!

I'm glad I have finally taken this plunge which was hardly a plunge at all. I know that I am only skimming the surface of AJAX.NET and ATLAS. But now I won't hesitate using them in any web projects going forward.

In fact, I am going to go add ATLAS to the Vermont.NET website. I've been unhappy with the bottleneck caused by populating my RSS feeds, since the RSS Toolkit I am using does not have an asynchronous implementation (hopefully that will change at some point). Just last night I removed the RSS Toolkit (in development, not in production) so that I could populate the datalists using ASP.NET 2.0's Asynchronous Tasks. This will certainly make my page load faster. However, with AJAX or ATLAS, I can now load the page and then go out and get the feeds and populate the datalists. This way, the user has something to look at while I'm populating the feeds. I will also do this with the display that shows the number of users from the Membership API. One of the big pitfalls of doing stuff with the membership API is that it always makes a round trip to the server.

posted on Monday, August 07, 2006 3:09 PM