ASP.NET
  Home arrow ASP.NET arrow Page 2 - Delving Deeper into Drag and Drop Programm...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Delving Deeper into Drag and Drop Programming in Microsoft ASP.NET AJAX
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2007-06-25

    Table of Contents:
  • Delving Deeper into Drag and Drop Programming in Microsoft ASP.NET AJAX
  • Write a Behavior: ShoppingCartBehavior to make the shopping cart droppable
  • Coding the web page
  • Obtaining the list of the pets to sell via Web Service
  • Dealing with the order via Web Service

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Delving Deeper into Drag and Drop Programming in Microsoft ASP.NET AJAX - Write a Behavior: ShoppingCartBehavior to make the shopping cart droppable


    (Page 2 of 5 )

    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:

     

    ShoppingCartNamespace.ShoppingCartBehavior = function(element) {

      //initialize the base class

      ShoppingCartNamespace.ShoppingCartBehavior.initializeBase(this, [element]);

     

      // pet list in the cart

      this._pets = new Object();

    }

     

    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.

     

    ShoppingCartNamespace.ShoppingCartBehavior.prototype = {

      //get the target to drop ( in this case-the shopping cart)

      get_dropTargetElement: function() {

        return this.get_element();

      },

      //Judge whether the draggable element can be dropped onto the target

      canDrop: function(dragMode, dataType, data) {

        return (dataType == "Pet" && data);

      },

      //Drop the draggable element onto the related target

      drop : function(dragMode, dataType, data) {

        if (dataType == "Pet" && data) {

          // since there doesn't exist this kind of article in the

          //shopping cart, set the value of field Quantity 1

          if (this._pets[data.Id] == null) {

            this._pets[data.Id] = {Pet: data, Quantity: 1};

          }

          //since there's already the article in the shopping cart

          //plus 1 to field Quantity

          else {

            this._pets[data.Id].Quantity++;

          }

          //Refresh the shopping cart UI

          this._refreshShoppingCart();

     

          //restore the old color of the shopping cart

          this.get_element().style.backgroundColor = "#fff";

        }

      },

      // call this function when the draggable element is located on the drop target

      onDragEnterTarget : function(dragMode, dataType, data) {

        if (dataType == "Pet" && data) {

          //set the color of the shopping cart to grey

          this.get_element().style.backgroundColor = "#E0E0E0";

        }

      },

      //call this function when the draggable element is away from the drop target

      onDragLeaveTarget : function(dragMode, dataType, data) {

        if (dataType == "Pet" && data) {

          // restore the old color of the shopping cart

          this.get_element().style.backgroundColor = "#fff";

        }

      },

      // call this function when the draggable element is being dragged on the drop
    target

      onDragInTarget : function(dragMode, dataType, data) {

      },

     

      //refresh the shopping cart with the product

      //list in the current shopping cart

      _refreshShoppingCart: function() {

        var cartBuilder = new Sys.StringBuilder();

        for (var id in this._pets) {

          cartBuilder.append("<div>");

          cartBuilder.append(this._pets[id].Pet.Name);

          cartBuilder.append(" * ");

          cartBuilder.append(this._pets[id].Quantity);

          cartBuilder.append("</div>");

        }

     

        this.get_element().innerHTML = cartBuilder.toString();

      },

     

      //Return the object holding the required product id and quantity

      getProductsToBeOrdered: function() {

        var productsToBeOrdered = new Object();

     

        for (var id in this._products) {

          productsToBeOrdered[id] = this._pets[id].Quantity;

        }

     

        return productsToBeOrdered;

      },

     

      // initialization

      initialize: function() {

        // initialize the base class

        ShoppingCartNamespace.ShoppingCartBehavior.callBaseMethod(this,
    "initialize");

     

        //register the drop target in the DragDropManager

        Sys.Preview.UI.DragDropManager.registerDropTarget(this);

      },

     

      //destruction function

      dispose: function() {

        //cancel registering the drop target in the DragDropManager

        Sys.Preview.UI.DragDropManager.unregisterDropTarget(this);

     

        ShoppingCartNamespace.ShoppingCartBehavior.callBaseMethod(this,
    "dispose");

      }

    }

     

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

     

    ShoppingCartNamespace.ShoppingCartBehavior.registerClass
    ("ShoppingCartNamespace.ShoppingCartBehavior",

      Sys.UI.Behavior, Sys.Preview.UI.IDropTarget);

     

    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:

     

    if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();

     

    More ASP.NET Articles
    More By Xianzhong Zhu


       · Whats ironic is I just recently created a drag/drop DLL control or a customer and...
       · Hi, I'm very glad to hear that and would like to be notified. In fact, the current...
       · Hey guys, many tnx for the examples - beginners like me can get the right impuls to...
       · Thanks a lot for showing the path to ajax - specials.Latawiec
       · You're welcome. Remember that I'm just a newbie for MS AJAX (only for 6 months in...
     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT