Click here to Skip to main content
15,860,972 members
Articles / Web Development / HTML
Article

Introduction to Anthem.NET

Rate me:
Please Sign up or sign in to vote.
4.76/5 (103 votes)
26 Feb 2006CPOL6 min read 611.3K   233   135
How to do AJAX without writing any JavaScript.

Introduction

AJAX 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:

  • support ASP.NET 2.0 as that's what I'm using,
  • soak as seamlessly as possible with VS2005, ideally without having to hand-code client-side JavaScript, and
  • support ASP.NET functionality as closely as possible.

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.Net

First, 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 VS2005

Assuming 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 Test

In 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 LinkButton and a Panel. Drag these from your *Anthem* toolbox. Set the 'Text' property of LinkButton1 to "Login". Now, drag an ASP.NET login control from the ASP.NET 'Login' tab, and place it inside Panel1. Using an Anthem Panel control is the simplest way of providing AJAX updates to non-Anthem controls, if needed. Set Panel1's property Visible to false, so that it isn't visible by default.

Page

Now the magic! Double-click the LinkButton on the designer. You should now be in the code editor, with a template LinkButton1_Click function. This is not a postback but a callback function.

Anthem will:

  1. create the necessary JavaScript on the client,
  2. bind it to the control's OnClick event,
  3. receive the XMLhttprequest on the server, and
  4. call the LinkButton1_Click in your server-side code.

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' UpdateAfterCallBack property to 'true'. If the control contains other non-Anthem controls (as our Panel1 does), then this will be updated. You can also pre-specify a control as UpdateAfterCallback=true in the designer, if you always want it to update.

Back to our demo - we add the following code to make the Panel1 visible and also update this back in the callback result:

C#
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 Panel1 visible, and returns the updated Panel1 control. Presto, the login control appears without a postback. Look ma, no JavaScript!

To show we have control states passed back, let's try another test. Back in the designer, drag a normal ASP.NET TextBox control onto the web page (below the Panel1 is fine). We will copy the value of this control into the UserName field of the login control when we make it visible, so amend the LinkButton code as follows:

C#
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 LinkButton and Button have two useful properties: TextDuringCallBack and EnabledDuringCallBack. If we set EnabledDuringCallback to false, the button is disabled during the callback processing time. To give the user some feedback, you can also set TextDuringCallBack=Wait so they know something's happening. If you run this on a local IIS, it's so quick you won't see it, so let's add a delay so it's easier to see:

C#
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;
}

Conclusion

I 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!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
Developer, business person, etc.

Comments and Discussions

 
GeneralYou are the bomb Pin
Layinka21-Mar-09 3:43
professionalLayinka21-Mar-09 3:43 
Generalmultiple anthem.gridview, updateaftercallback not working Pin
Raj Kumar Mittal22-Dec-08 22:38
Raj Kumar Mittal22-Dec-08 22:38 
GeneralLooks great! Pin
Jorge Bay Gondra22-Aug-08 3:25
Jorge Bay Gondra22-Aug-08 3:25 
QuestionIs this still the best way in 2008? Pin
basementjack2-Jul-08 8:58
basementjack2-Jul-08 8:58 
AnswerRe: Is this still the best way in 2008? Pin
Howard Richards2-Jul-08 22:58
Howard Richards2-Jul-08 22:58 
It's fully compatible with 3.5 (I am using it with VS2008 now) and I've had no major issues with it - most of the 3.5 updates are language oriented and the web functionality has not changed significantly.

It's really down to preference.. I still use it and not MS Atlas/AJAX because I find it simpler to use. It's not as powerful as all the custom controls - I'd like to use some of those sexy expanding and other Atlas controls now available, so maybe I'll swap in future, but for the present I'm sticking with it.

'Howard

GeneralProblem in IE6 Pin
Priyatam K20-Nov-07 1:37
Priyatam K20-Nov-07 1:37 
GeneralRe: Problem in IE6 Pin
Howard Richards20-Nov-07 2:41
Howard Richards20-Nov-07 2:41 
GeneralRe: Problem in IE6 [modified] Pin
Priyatam K21-Nov-07 7:35
Priyatam K21-Nov-07 7:35 
QuestionRe: Problem in IE6 Pin
Priyatam K17-Dec-07 19:52
Priyatam K17-Dec-07 19:52 
GeneralRe: Problem in IE6 Pin
Howard Richards17-Dec-07 22:00
Howard Richards17-Dec-07 22:00 
QuestionRe: Problem in IE6 Pin
Priyatam K19-Dec-07 0:53
Priyatam K19-Dec-07 0:53 
GeneralRe: Problem in IE6 Pin
Howard Richards20-Dec-07 23:11
Howard Richards20-Dec-07 23:11 
GeneralRe: Problem in IE6 Pin
Priyatam K26-Dec-07 20:42
Priyatam K26-Dec-07 20:42 
QuestionWhat about Flicker problems ? Pin
sacheen19847-Nov-07 20:36
sacheen19847-Nov-07 20:36 
AnswerRe: What about Flicker problems ? Pin
sacheen19847-Nov-07 21:27
sacheen19847-Nov-07 21:27 
GeneralRe: What about Flicker problems ? Pin
Howard Richards7-Nov-07 23:52
Howard Richards7-Nov-07 23:52 
GeneralRe: What about Flicker problems ? Pin
sacheen19848-Nov-07 0:21
sacheen19848-Nov-07 0:21 
GeneralRe: What about Flicker problems ? Pin
Howard Richards8-Nov-07 1:33
Howard Richards8-Nov-07 1:33 
GeneralRe: What about Flicker problems ? Pin
sacheen19848-Nov-07 17:59
sacheen19848-Nov-07 17:59 
GeneralRe: What about Flicker problems ? Pin
Howard Richards8-Nov-07 21:48
Howard Richards8-Nov-07 21:48 
GeneralAnthem Panel Control Pin
Praveen02914-Oct-07 23:50
Praveen02914-Oct-07 23:50 
GeneralRe: Anthem Panel Control Pin
Howard Richards15-Oct-07 1:56
Howard Richards15-Oct-07 1:56 
Questiongoogle like calendar Pin
Attila Balogh13-Oct-07 21:57
Attila Balogh13-Oct-07 21:57 
AnswerRe: google like calendar Pin
Howard Richards14-Oct-07 23:45
Howard Richards14-Oct-07 23:45 
Questionanthem.net Pin
manojom3-Oct-07 19:28
manojom3-Oct-07 19:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.