In this article, we will be looking at the AJAX Toolkit from Microsoft. This article is directed at users of Visual Studio.NET 2008, but users of the 2005 version may also take part, but will be required to install the AJAX Extensions from Microsoft.

The AJAX Control Toolkit is a group of AJAX Extenders that was released by Microsoft to make it easier for us to implement seemingly complex functions. Microsoft have made it very easy for us to implement into our web applications such dynamic features as rating systems, modal popup windows, and dynamic searching of a listbox. During this article, we will be looking at these implementations of AJAX a little more closely, but to do so, you will need to download the AJAX Control Toolkit from this link.

If you need any help with installing the Toolkit for VS.NET 2005, please view this article.

When implementing AJAX in an ASP.NET Web Application, the first thing we need to do is add a ScriptManager to the page. The ScriptManager will manage all of our AJAX calls for us, and will look something like this:

We should have our AJAX Toolkit in our toolbox in VS.NET, and should be able to just drag and drop any of the extenders into our ASPX page. The first we will be taking a look at will be the TextBoxWatermarkExtender. This will extend any TextBox on your page to include a Watermark of sorts that will be displayed when the textbox is empty. When the user clicks on the textbox, the watermark will disappear – only to re-appear if the textbox is still empty when it loses focus.

To use this extender, let’s first add a textbox to our ASPX page:

Next, we can drag onto our ASPX page the Extender. When we do this, you should notice two things. Number one should be the assembly reference for the Custom Control, which is added at the top of the page, and two, the Extender itself is added where you dragged it onto.

The assembly reference will look something like this, just below the page directive:

And the Extender itself should look something like this:

Notice that the only attributes that are required for this extender are to specify the TextBox control that it will be assigned to, and specify the text to be displayed as a Watermark. An optional third attribute is to use a CSS Class for the Watermark. In this example, we use the following CSS style:

Now if you run this page you will see the Watermark displayed in the textbox on page load. The watermark will disappear when you click the textbox and enter some text. However, if you delete that text and then click off of the textbox (losing its focus), the Watermark will re-appear. Notice that all of this happens without a delay – in true AJAX style.

The next Extender we can demonstrate is the ModalPopupExtender. With this control, we can set a Control to popup, and have the rest of the page disabled. This is useful when generating confirmation messages, or warnings, etc.

For this example, we will create a Panel to popup on a button click. Let’s go ahead and add a Button and a Panel to our ASPX page:

Notice we specify a CSS Class for the panel, to make it stand out more. We also create a class for the background, which will cover the whole page when the popup is visible. We will assign this class to the ModalPopupExtender shortly. The CSS Classes look like this:

Now we can drag onto our ASPX page the ModalPopupExtender, which will look something like this:

Notice on this Extender we have three controls to reference: which will initiate the popup, which will be the popup, and which will act as the popup Ok control. We set the Panel to be the popup and the Button to initiate it. We also have an OK button in our panel, so this will close the panel. There is also the option of specifying a Cancel button for the popup. Finally, we add the CSS Class to the popup and we can then run it.

Notice that when we click the Confirm button, we are presented with a very nice popup and the rest of the page is disabled and grayed out:

When the OK button is clicked, we are returned to the normal page. Again, the functions are instantaneous and are more akin to a desktop application than a web application.
The next Extender we will be looking at is the ListSearchExtender. This can be a really useful tool, particularly when working with large, unordered data in a ListBox control. In order to successfully implement this extender, we are going to add a ListBox Control and fill it with sample data:

Now we add our Extender from the toolbox:

This extender has a lot of power, and yet, we are only required to associate the ListBox with it and boom – instant search capabilities. Now when you run this web application, you’ll be able to click on any item in the ListBox and start typing. As you type, the closest match will be selected automatically:

The final Extender we will be looking at in this article is Rating. This little control will allow your users to quickly and easily select a score from a graphical interface, which you can fully customize. This control requires four different CSS Classes by specified: FilledStarCssClass, EmptyStarCssClass, WaitingStarCssClass, and StarCssClass. We create separate classes for these and add them to the ASPX page:

We created simple star graphics for this example and used them in the CSS classes. Next, we will also add an UpdatePanel and a Literal control to the ASPX page, in addition to the Rating control:

We will use the Literal control to display the selected rating, and the UpdatePanel to force an Asynchronous Postback. To do this, we also set the AutoPostBack attribute of the Rating control to true. And finally, in the code-behind, we set the value of the Literal control, using the Changed event of the Rating:

We use the MaxRating attribute of the Rating control instead of hard-coding 5 (or 10) because the Rating can be easily changed by setting the MaxRating attribute in the ASPX page.

That’s all for this article, but the AJAX Toolkit has plenty of extenders to utilize. Don’t be afraid to just dive in and check them out.

Download Source File