Published: 02 Oct 2006
By: Alessandro Gallo

The MS AJAX framework provides an event model similar to the .NET framework's one. We can create, raise and handle events in a manner similar to what happens in a code-behind file of an ASP.NET page. Plus, the MS AJAX framework allows to attach pieces of re-usable client-side functionality through the concept of behaviors. Let's give an example of these concepts with the help of a little example.

Introduction

The MS AJAX framework provides an event model similar to the .NET framework's one. We can create, raise and handle events in a manner similar to what happens in a code-behind file of an ASP.NET page. Plus, the MS AJAX framework allows to attach pieces of re-usable client-side functionality through the concept of behaviors. Let's give an example of these concepts with the help of a little example.

Scenario

The simple MS AJAX behavior that we are going to code is called CustomClickBehavior and, when attached to a MS AJAX control, allows to subscribe to four events:

  • mouseDown, is fired when the user presses one of the mouse buttons into the area occupied by the control's associated DOM element.
  • mouseUp, is fired when the user releases one of the mouse buttons into the area occupied by the control's associated DOM element.
  • leftClick, is fired if the user clicks the left mouse button.
  • rightClick, is fired if the user clicks the right mouse button.

This behavior, that could be easily extended (for example to detect double-clicks or mouse wheel movements), shows how to create, raise and handle events in the MS AJAX framework.

The first thing to do is creating a new JavaScript file and name it CustomClickBehavior.js. Then, drop the following code:

Code explanation

The code for the CustomClickBehavior starts with the creation of a namespace called DotNetSlackers, that will contain the behavior class. Notice that the behavior (as any other MS AJAX class) is declared using its full qualified name (i.e. prefixing its name with the name of the container namespace).

The initializeBase() method (we use the term "method" since we are dealing with the MS AJAX OOP patterns for JavaScript) must be called by every class that has a parent class. Every behavior inherits from the base Sys.UI.Behavior class, and the inheritance relationship is specified in the call to the registerClass() method, that is the first statement outside the body of the class. The parent class is the second argument passed to the registerClass() method.

To create an event, we use the object of type Event that is returned by the createEvent() method. This method is provided by every MS AJAX component (a component is a class that inherits from the base Sys.Component class and a behavior is a MS AJAX component) and the returned object is assigned to a particular class member that, at this point, represents a MS AJAX event.

In the initialize() method, we are hooking-up the mousedown and mouseup events raised by the DOM element that the associated control is wrapping. Notice how we are declaring the event handler by first creating a delegate with the createDelegate() method and then assigning the returned reference to a class member. Then, we are using this reference to attach the handler to the DOM event through the attachEvent() method called on the DOM element wrapped by the associated control. This approach, that is the recommended one, allows to handle DOM events by preventing the potential memory leaks that could be caused by an improper use of JavaScript closures. Finally, notice how we are effectively wrapping a DOM event with a MS AJAX event (for example, the mouseUp event is wrapping the mouseup DOM event).

The two methods that handle the DOM events, mousdownHandler() and mouseupHandler(), are used to raise the corresponding MS AJAX events (mouseDown and mouseUp) after performing some elaborations. Specifically, in the mouseupHandler() method we are testing against the button property of the event object (that is accessed in all the supported browsers through the window.event reference) to determine whether to raise the leftClick or rightClick event.

To be able to handle these four events with the declarative code (xml-script) we have to override the getDescriptor() method and use the addEvent() method on the type descriptor instance returned, like we did in the code snippet above.

To complete this overview of the MS AJAX event model, let's code a simple ASP.NET page that traces the events raised by the MS AJAX framework when the user clicks a simple HTML button:

Regarding the declarative xml-script section, having provided a type descriptor for our behavior allows to map the MS AJAX events to XML attributes whose value is a reference to a JavaScript function (declared in the JavaScript block that follows the xml-script one) that is responsible for handling that particular event.

Conclusions

The above code snippet is a piece of reusable client-side code that we can attach to any MS AJAX control to raise custom events when the user clicks on the associated DOM element. We've also seen how the MS AJAX event model allows to raise and handle events in a manner similar to the .NET framework.

<<  Previous Article Continue reading and see our next or previous articles Next Article >>

About Alessandro Gallo

Alessandro "Garbin" Gallo is a Microsoft MVP in the Visual ASP/ASP.NET category and a .NET developer/consultant. He is a contributor for the Ajax Control Toolkit project, owned by Microsoft. Alessandro won the Grand Prize at the "Mash-it-up with ASP.NET AJAX" contest held by Micr...

This author has published 23 articles on DotNetSlackers. View other articles or the complete profile here.

Other articles in this category


Animating a Web Form using ASP.NET AJAX
In this article you will learn how to animate a webform using asp.net ajax.
Jquery Ajax
JQuery makes communicating with the server very, very easy. JQuery uses get(), getJSON(), and post(...
jQuery in Action 2nd edition: Queuing functions for execution
This article is taken from the book jQuery in Action, second edition. This segment shows how you can...
Test120Jan
This is my custom article
jQuery Deferred Objects Promise Callbacks
jQuery Deferred/Promise objects untangle spaghetti-like asynchronous code.
Top
 
 
 

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.