|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionAJAX is something lots of ASP.NET developers are interested in, but there are a number of AJAX libraries and solutions out there - so which one do you go for? When looking myself, the criteria I used were:
Having looked at the various AJAX libraries out there (including Atlas), I noticed that the ASP.NET forums were using a library I hadn't seen before (and it's not Atlas!). Web searches led me to Jason Diamond's MyAjax.net which has recently been redesigned and launched on SourceForge.Net as Anthem.NET, so it's now an official OpenSource project. There are also some discussion forums. Note: It was pointed out in the comments that I had not addressed the issue of cross-browser compatibility, which is obviously quite important! I'm glad to report that Anthem.NET works on IE, FireFox, and Safari (reportedly much better than Atlas, as I write). The developer has even put work into making sure it runs under Mono as well. Just Another AJAX Library?So, is Anthem just another AJAX library? No, and I'd even venture [tin helmet on] it's the best one I've seen for ASP.NET. Anthem is so easy to use, it's almost a crime. It ticked all my criteria (it supports both .NET 1.1 and .NET 2.0), and has a number of built-in AJAX-enabled controls. It supports viewstate, so you can get control states and page info back in callbacks. It works seamlessly with web usercontrols, and master pages; and having used it on a live application, I can say it's reasonably stable. Best of all, you won't need to write any JavaScript (or XMLscript). Getting Athem.NetFirst, you need to download Anthem.NET 1.0, which you will find from the links on the main page here. Unfortunately, the DLL is not precompiled in the snapshots, so you will need to extract the projects and compile them to get the DLL. There are two versions of Anthem, one each for VS2003, and one for VS2005. You'll also find that the Anthem snapshot has a demonstration website included in the file, that demos the various controls and how to use them. Using Anthem in VS2005Assuming you've done this and have created the DLL, all you need to do to install Anthem.NET in a web application is add the DLL as a reference to your web application. You can then make the Anthem controls available by creating a new tab in the Toolbox, and use the right-click menu "Choose Items" - click Browse and locate the Anthem DLL. All the Anthem controls are essentially subclassed ASP.NET controls, so you can use them on a web page just as you would a normal ASP control. In general, if you don't need AJAX functionality, it's better to use the normal ASP.NET controls, as these won't have the HTML overhead. A Simple First TestIn our first demo page, we are going to use Default.aspx which should have been created for you when you created the website. Let's say, we want to have a "login" button on this page, but we don't want to redirect to another page which shows the login control, we want to unhide a login control so that the response is faster. If we use a traditional ASP.NET postback, the whole page has to be reloaded to make this visible. To do this with Anthem, we need two Anthem controls: a
Now the magic! Double-click the Anthem will:
Not only this but you have full viewstate, page and control states loaded. Indeed, you can treat Anthem callbacks exactly like a postback, with only one exception: the only client controls updated are those you specify. To do this, you have to set the Anthem controls' Back to our demo - we add the following code to make the protected void LinkButton1_Click(object sender, EventArgs e)
{
// toggle Panel1 visibility
this.Panel1.Visible = ! this.Panel1.Visible;
// tell Anthem to update this control
this.Panel1.UpdateAfterCallBack = true;
}
Now, try running this. The page comes up with just the login link. Click the login link, and the callback is sent to the server, which makes To show we have control states passed back, let's try another test. Back in the designer, drag a normal ASP.NET protected void LinkButton1_Click(object sender, EventArgs e)
{
// set username from textbox
this.Login1.UserName = this.TextBox1.Text;
// toggle Panel1 visibility
this.Panel1.Visible = ! this.Panel1.Visible;
// tell Anthem to update this control
this.Panel1.UpdateAfterCallBack = true;
}
When you run the code this time, enter some text in the textbox before you click Login. Note, this is passed back in the viewstate, and is available to our callback code. Another refinement: what if we have a slow connection? the user might click Login to get the page, but a second or two's delay might make them think nothing has happened (especially if the server is loaded). They might click it again, and cause the login control to flick on/off. It's similar to the Order Submit problem where you only want the user to click the Submit button once. Anthem controls like protected void LinkButton1_Click(object sender, EventArgs e)
{
// simulate network delay with 2 sec delay
System.Threading.Thread.Sleep(2000);
// set username from textbox
this.Login1.UserName = this.TextBox1.Text;
// toggle Panel1 visibility
this.Panel1.Visible = ! this.Panel1.Visible;
// tell Anthem to update this control
this.Panel1.UpdateAfterCallBack = true;
}
ConclusionI hope this article has whetted your appetite. Anthem.NET is the library that Microsoft should have written, and having looked at Atlas, I found it too complex. It's such an easy way to add AJAX functionality to an ASP.NET website that I'm sure you'll find it as useful as I am already doing!
|
||||||||||||||||||||||