HomeASP.NET Delving Deeper into Drag and Drop Programm...
Delving Deeper into Drag and Drop Programming in Microsoft ASP.NET AJAX
In the first part of this article, we summarized the multiple solutions suggested by MS AJAX, and began building an example to show some of these solutions in action. In the conclusion, we continue exploring this rather complex example to show you the under-the-hood workings of all the client side drag and drop solutions within the MS AJAX framework.
Contributed by Xianzhong Zhu Rating: / 8 June 25, 2007
Define a Behavior: DraggableProductBehavior to drag the product
With the analysis I gave you before, you can see that this DraggableProductBehavior is virtually a class that has to implement the Sys.Preview.UI.IDragSource interface. We'll attach this behavior to the related HTML DOM element that identifies the product to make it draggable.
Now, just create a new JavaScript file named ShoppingCart.js, and we should register the namespace ShoppingCartNamespace:
Type.registerNamespace("ShoppingCartNamespace");
Next, define the constructor of this class, as well as some necessary fields:
Here, we give a typical MS AJAX styled JavaScript definition for a class prototype. Since there are detailed explanations accompanying the above code, we won't cover it any further, while there's one more word to be added -- note how we have implemented each method of the IDragSource interface and tell the DragDropManager to start the dragging operation.
Finally, you have to register the DraggableProductBehavior's behavior into the MS AJAX client-side framework:
This behavior -- ShoppingCartBehavior, as we imagined beforehand, must implement the Sys.Preview.UI.IDropTarget interface. We will attach this behavior to the related HTML DOM element that represents the shopping cart and make it accept the draggable object.
Still, let's first define the constructor as well as some related fields:
Now we come to the prototype definition of ShoppingCartBehavior. Still for brevity, we'll omit the related explanations, but you should also note how we implement the methods of the IDropTarget interface and how the DragDropManager deals with registering/unregistering the dropped object.
Similarly, you should register ShoppingCartBehavior's behavior into the MS AJAX client-side framework (you can see that this behavior has implemented the IDropTarget interface):
Last but not the least; please remember to call the notifyScriptLoaded() method of Sys.Application at the end of the ShoppingCart.js file to notify the ASP.NET AJAX client-side framework that this script has been loaded successfully:
In Step 1, we've only browsed the snapshot of the Default.aspx web page. Now, let's dig into its related coding.
First, we have to add the necessary references to the relevant script files --PreviewScript.js, PreviewDragDrop.js, and ShoppingCart.js, as well as the Web Service-ShoppingService written a moment ago:
In this sample, after the client side has been initialized, we will invoke the server-side Web Service asynchronously to acquire the information about all the pets on the online store, as well as add a ShoppingCartBehavior action to the shopping cart, all of which should be performed within the pageLoad function (this function is a global built-in function defined by MS AJAX, which is automatically executed after the client-side framework has been initialized successfully, within which we can perform some initializations).
function pageLoad(sender, args) {
// call Web Service to get all the pets related info
ShoppingService.GetPets(onPetsGot);
// add behavior ShoppingCartBehavior to the shopping cart
$create(
ShoppingCartNamespace.ShoppingCartBehavior,
{"name": "myShoppingCartBehavior"},
null,
null,
$get("shoppingCart")
);
}
Here, inside the call back function onPetsGot(), we'll, according to the pet set returned from the web service, create each element corresponding to the pets within the container dynamically. And also, we should add the DraggableProductBehavior behavior to each element inside the container-petContainer.
function onPetsGot(result) {
// first get the container to display all the pets
var petContainer = $get("petContainer");
// iterate through the pet collection returned from the server
for (var index = 0; index < result.length; ++ index) {
// current pet
var thisPet = result[index];
//Create a new DOM element-div according to current pet info, and add it to the pet container
When the customer finally clicks the Order button near the shopping cart, he can see a message showing the data result he cares about within a red rectangle below the shopping cart area. This is accomplished by asynchronously posting back the current information about the pets to buy that needs to be processed to the server side. The following code corresponds to the click event handler of the Order button:
function btnOrder_onclick() {
//get behavior ShoppingCartBehavior attached to the shopping cart
var shoppingCartBehavior = Sys.UI.Behavior.getBehaviorByName(
$get("shoppingCart"),
"myShoppingCartBehavior"
);
//Get the Id and quantity of each pet in the shopping cart
As is seen from above, we first use the Sys.UI.Behavior.getBehaviorByName() method to get the behavior-ShoppingCartBehavior that is attached to the shopping cart, then obtain the Id and quantity of each pet in the shopping cart, and finally post this information back to the Web Service to be processed.
Here, still for demonstration purposes, in the call back function productsToBeOrdered() we've used an HTML element div to display the response from the server to the user:
function onOrdered(result) {
OutputMsg.innerHTML=result;}
So much for this sample; you can press F5 and give it a test! I bet you will be attracted by the friendly interface.
Final Thoughts
In this article, we first briefly compared most of the drag and drop solutions supplied by the MS AJAX framework, then summed up the inner workings of the drag and drop mechanism on the client side. After that, we focused on the client-side interfaces IDragSouce and IDropTarget, and also the DragDropManager and gave an integrated and complex example. Finally, we should notice that the MS AJAX framework has been undergoing rapid and great changes day after day; thus, only by keeping up with the changes can we web developers create the most professional, attractive and cross-browser compatible web applications.