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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

    - More Advanced ASP.NET 3.5 Functions and Subr...
    - ASP.NET 3.5 Functions and Subroutines
    - Coding an IQ Test with Conditionally Driven ...
    - Developing Conditionally Driven Event Handle...
    - ASP.NET 3.5 Debugging Using Visual Web Devel...
    - Understanding Event Handlers in ASP.NET 3.5
    - Building a Web Form in ASP.NET and PHP: a Co...
    - Inserting Data into a Microsoft SQL 2008 Dat...
    - Creating an ASP.NET Dynamic Web Page Using M...
    - Retrieving Data from Microsoft SQL Server 20...
    - Building ASP.NET Web Forms to Use a MySQL Da...
    - Creating an ASP.NET Database using MS SQL 20...
    - Building an ASP.NET Website Using Include Ta...
    - Create ASP.NET Web Forms to Use a Microsoft ...
    - Editing Web Design Layout in Visual Web Deve...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek