Welcome to AspAdvice Sign in | Join | Help

Alessandro Gallo

.NET & Beyond
Introduction to Drag And Drop with Atlas
Quick overview
The Atlas framework provides cross-browser support for drag&drop operations. Basically, to create a UI with drag&drop capabilities we need

  • draggable items and drop targets. Draggable items are controls that can be moved around the page, while drop targets are controls that act as "containers" for draggable items. The Atlas framework allows to define draggable controls by implementing the IDragSource interface. A drop target is, instead, a class that implements the IDropTarget interface. It is also possible to create a class that implements both the IDragSource and the IDropTarget interfaces: we'll have a control that acts both as a draggable item and a drop target (an example of such control is a "floating" control).
  • a DragDropManager, which is an object with global scope that is instantiated at runtime. This object can be accessed through the reference Web.UI.DragDropManager and is generally used to start dragging operations and to register drop targets. From there on, the DragDropManager handles all the drag&drop operations through calls to IDragSource and IDropTarget methods.
So, a very basic flow needed to create a drag&drop UI could be
  • create draggable items by implementing the IDragSource interface. The class that implements this interface is also responsible to call the Web.UI.DragDropManager.startDragDrop() method to start the dragging operation (tipically, this is done in an event handler for the mousedown event of the control's element). Each draggable item has its own dataType, i.e. an identifier that allows to group draggable items (the predefined dataType is HTML);
  • create drop targets by implementing the IDropTarget interface. A class that implements this interface is responsible to register the drop target by invoking the Web.UI.DragDropManager.registerDropTarget() method. Each drop target has a list of acceptedDataTypes, that specifies which "types" of draggable items can be dropped on that target.

In summary, a drag&dop operation is tipically started by an IDragSource object with the call to the startDragDrop() method of the DragDropManager. Then, the operation is handled by the DragDropManager, through calls to the IDragSource methods of the item being dragged, and IDropTarget methods of the registered drop target(s).

Builtin classes for Drag&Drop
Before going into the drag&drop engine details (I'll talk about the IDragSource and IDropTarget interfaces in another post) it's better to look at some builtin controls and behaviors provided by the Atlas framework to perform drag&drop operations. Obviously, all these classes implement the IDragSource, IDropTarget interface, or both.
  • The DragDropList behavior allows to add drag&drop capabilities to list of controls. One tipical use is to add this behavior to a ListView control.
  • The DraggableListItem behavior allows to define a draggable item in a DragDropList. Usually it is added as an ItemTemplate's behavior inside a ListView, to make list items draggable.
  • The DataSourceDropTarget behavior is used to add dropped data to a DataSource control.
  • The FloatingBehavior can be added to a control to obtain a floating control.
The example
In the example, we'll build a simple page with draggable content (you could give a look to Start or Windows Live page for more complex examples of this kind of layout and functionality) by using the DragDropList and DraggableListItem behaviors. Another example using the DragDropList and other drag&drop examples can be found in Wilco's website. You can found the full code for this example at the end of this post.

First of all, let's create the page with static layout (think of it as a "screenshot" of the final dynamic layout). Then, we'll add dynamic behaviors to controls with some Atlas markup. I've created two zones that are supposed to act as drop zones for draggable content. The draggable content is represented by panels with ASP.NET controls inside them.

<!-- Left Area -->
<div id="leftArea" class="list1">

    <div id="content1" class="item">
        <div id="content1Handle" class="itemHandle">Content 1</div>
        <div class="itemContent">
            <asp:Login ID="myLogin" runat="server"
                       CssClass="centered"></asp:Login>
        </div>
    </div>
   
    <div id="content2" class="item">
        <div id="content2Handle" class="itemHandle">Content 2</div>
        <div class="itemContent">
            <asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>
            <asp:Button ID="myButton" runat="server"
                        Text="ClickMe" />
        </div>
    </div>

</div>

<!-- Right Area -->
<div id="rightArea" class="list2">

    <div id="content3" class="item">
        <div id="content3Handle" class="itemHandle">Content 3</div>
        <div class="itemContent">
            <asp:Calendar ID="myCalendar" runat="server"
                          CssClass="centered"></asp:Calendar>
        </div>
    </div>

</div>

As you can see, I've created two drop zones and three content panels inside them, obtaining the static layout for the page.
Now, we need some HTML to represent the "highlighted" content area when a panel is dragged over (the dropCueTemplate), and some HTML to display when all the panels have been moved away from a drop zone (the emptyTemplate).

<!-- Hide template elements -->
<div class="templates">
    <!-- DropCue Template -->
    <div id="dropCueTemplate" class="dropCue"></div>
    <!-- Empty Template -->
    <div id="emptyTemplate" class="emptyList">Drop content here.</div>
</div>

Ok, we are ready to make this a dynamic page by adding Atlas markup. The two drop zones will become two Atlas controls with DragDropList behavior. The code for the left drop area is

<!-- Left Area -->
<control id="leftArea">
    <behaviors>
        <dragDropList dataType="HTML"
                      acceptedDataTypes="'HTML'"
                      dragMode="Move"
                      direction="Vertical">
            <dropCueTemplate>
                <template layoutElement="dropCueTemplate" />
            </dropCueTemplate>
            <emptyTemplate>
                <template layoutElement="emptyTemplate" />
            </emptyTemplate>
        </dragDropList>
    </behaviors>
</control>

while the code for the right area is similar. In the above code, we have wrapped the "leftArea" <div> element in an Atlas control, and added a DragDropList behavior to it. This DragDropList holds data of type "HTML", accepts data of  type "HTML", has a Vertical orientation (the directon can be Horizontal or Vertical) and the drag operation is done by moving the whole item (the other option is dragMode="Copy", that drags a copy of the item). Inside the <dragDropList> element we have specified, by providing their IDs, which DOM elements to use as the dropCueTemplate and the emptyTemplate.

Let's see the markup for one of the content panels (the code for the other panels is similar):

<!-- Draggable items -->
<control id="content1">
    <behaviors>
        <draggableListItem handle="content1Handle" />
    </behaviors>
</control>

Again, the DOM element "content1" becomes an Atlas control with a draggableListItem behavior. This behavior allows the content panel to be dragged. The handle attribute contains the ID of an element that acts as the "grip" for the drag operation. I've set the handle to be the header of each content panel.
Finally, it's time to run the example. Here's the full source:

DragDropExample.aspx

<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>DragAndDrop UI Example</title>
    <atlas:ScriptManager ID="scriptManager" runat="server">
        <Scripts>
            <atlas:ScriptReference ScriptName="AtlasUIDragDrop" />
        </Scripts>
    </atlas:ScriptManager>
    <style type="text/css">
        body{font-family:Verdana;font-size:11px;}
        .main{position:relative;width:710px;height:540px;margin:auto;}
        .list1{position:absolute;left:0px;height:520px;width:340px;
               padding:10px 5px 10px 10px;}
        .list2{position:absolute;right:0px;height:520px;width:340px;
               padding:10px 10px 10px 5px;}
        .item{background:#fff;margin-bottom:5px;background:#fff;}
        .itemContent{padding:5px;text-align:center;}
        .itemHandle{height:15px;background:#e5ecf9;overflow:hidden;
                    border-top:solid 1px #3366cc;font-weight:bold;
                    cursor:move;}
        .dropCue{border:dashed 1px #ff0000;margin-bottom:5px;}
        .emptyList{font-weight:bold;text-align:center;}
        .centered{margin:auto;}
        .templates{visibility:hidden;}
    </style>
</head>
<body>
    <form id="form1" runat="server">
   
    <div class="main">
       
        <!-- Left Area -->
        <div id="leftArea" class="list1">
            <div id="content1" class="item">
                <div id="content1Handle" class="itemHandle">Content 1</div>
                <div class="itemContent">
                    <asp:Login ID="myLogin" runat="server"
                               CssClass="centered"></asp:Login>
                </div>
            </div>
           
            <div id="content2" class="item">
                <div id="content2Handle" class="itemHandle">Content 2</div>
                <div class="itemContent">
                    <asp:TextBox ID="myTextBox" runat="server"></asp:TextBox>
                    <asp:Button ID="myButton" runat="server"
                                Text="ClickMe" />
                </div>
            </div>
        </div>
        <!-- Right Area -->
        <div id="rightArea" class="list2">
            <div id="content3" class="item">
                <div id="content3Handle" class="itemHandle">Content 3</div>
                <div class="itemContent">
                    <asp:Calendar ID="myCalendar" runat="server"
                                  CssClass="centered"></asp:Calendar>
                </div>
            </div>
        </div>
        <!-- Hide template elements -->
        <div class="templates">
            <!-- DropCue Template -->
            <div id="dropCueTemplate" class="dropCue"></div>
            <!-- Empty Template -->
            <div id="emptyTemplate" class="emptyList">Drop content here.</div>
        </div>
   
    </div>
   
    </form>
    <script type="text/xml-script">
        <page>
            <components>
               
                <!-- Left Area -->
                <control id="leftArea">
                    <behaviors>
                        <dragDropList dataType="HTML"
                                      acceptedDataTypes="'HTML'"
                                      dragMode="Move"
                                      direction="Vertical">
                            <dropCueTemplate>
                                <template layoutElement="dropCueTemplate" />
                            </dropCueTemplate>
                            <emptyTemplate>
                                <template layoutElement="emptyTemplate" />
                            </emptyTemplate>
                        </dragDropList>
                    </behaviors>
                </control>
               
                <!-- Right Area -->
                <control id="rightArea">
                    <behaviors>
                        <dragDropList dataType="HTML"
                                      acceptedDataTypes="'HTML'"
                                      dragMode="Move"
                                      direction="Vertical">
                            <dropCueTemplate>
                                <template layoutElement="dropCueTemplate" />
                            </dropCueTemplate>
                            <emptyTemplate>
                                <template layoutElement="emptyTemplate" />
                            </emptyTemplate>
                        </dragDropList>
                    </behaviors>
                </control>
               
                <!-- Draggable items -->
                <control id="content1">
                    <behaviors>
                        <draggableListItem handle="content1Handle" />
                    </behaviors>
                </control>
                <control id="content2">
                    <behaviors>
                        <draggableListItem handle="content2Handle" />
                    </behaviors>
                </control>
                <control id="content3">
                    <behaviors>
                        <draggableListItem handle="content3Handle" />
                    </behaviors>
                </control>
               
            </components>
        </page>
    </script>
</body>
</html>

UPDATES:
04/04/2006 - Updated the code to the March CTP.

Sponsor
Posted: Tuesday, January 17, 2006 1:09 PM by Garbin

Comments

sam said:

Hi, great stuff!

One question, where you learn this king of stuff ? I dont see any explanation in the atlas website !!!

Thanks
# January 23, 2006 6:00 AM

Garbin said:

best places to find documentation are the blogs listed in the Atlas homepage (http://atlas.asp.net), Wilco Bauwer's site (http://www.wilcob.com) and the quickstarts/hands on lab tutorials (again, in the Atlas homepage).
# January 23, 2006 6:13 PM

matt said:

I think this is great. How could I go about making it more dynamic, though? For example, I want to dynamically create the handle div and the content div and still make them draggable. How could I achieve this, but on the fly? Thanks.

<control id="content1">
<behaviors>
<draggableListItem handle="content1Handle"
dataType="HTML" />
</behaviors>
</control>
# February 28, 2006 12:48 PM

Garbin said:

Matt,

one solution is to create an Atlas-enabled server control, i.e. a server control that implements the IScriptControl interface. These controls are able to render their Atlas markup on page. I'm planning to address this topic in one of my next posts.
# March 2, 2006 8:06 PM

Matt said:

I know you said you were going to address this topic in a future post, but do you have any examples of creating an Atlas-enabled server control that I could take a look at. Thanks.
# March 9, 2006 3:37 PM

Atlas notes said:

In a previous post I gave a quick introduction to the Atlas Drag&amp;amp;Drop system. The core of this system...
# March 29, 2006 6:21 AM

Pascal said:

Thank you for your post. As an ASP beginner, I had a look at your demo to get acquainted with Atlas' DND capabilities.

Nevertheless, I also tested it with dragMode set to "Copy" on at least one of the areas. But then *nothing* gets copied.

Any clues why? Thanks again.
# April 12, 2006 6:48 PM

Piter said:

Hi!

Thanks a lot for the example.

Would you happen to know why it doesn't work under Apache HTTP server with mod_aspdotnet?

Both Apache and IIS generate the exact same HTML but dragging&dropping doesn't work on the page from Apache. How this can be?

Thank you very much in advance.
# April 20, 2006 2:44 PM

Garbin said:

Piter,

I'm unsure of what could be the problem, because I've never tested Atlas with an Apache server.
By the way, if you found something interesting about this, you could drop a comment here. Thank you.
Garbin
# May 2, 2006 4:01 AM

Garbin said:

Pascal,
I haven't done any tests with the dragNdrop mode set to Copy. Did you solve your problem?
Garbin
# May 2, 2006 4:25 AM

Chaz said:

Wow this looks really interesting.  

I have a project coming up that has a loose requirement for dragdrop capability.  This may be a great solution, but.....

Will this work in drag/dropping of files (xml)?

I don't find the System.Web.UI.DragDropManager.  Am I missing something?
# May 3, 2006 11:01 AM

Garbin said:

Chaz,
if you are interested in Atlas dragNdrop capabilities, please check my post about dragNdrop interfaces.

In the April CTP, the DragDropManager reference is Sys.UI.DragDropManager.
Garbin
# May 3, 2006 12:36 PM

nonBot said:

I found some code here, http://www.nikhilk.net/AtlasM1.aspx, to save the position of objects; however it doesn’t seem to apply to this example. Please help!
# May 3, 2006 7:10 PM

Garbin said:

nonBot, have you tried to follow this tutorial: http://atlas.asp.net/docs/atlas/doc/appservices/profile.aspx
it may give you useful hints on how to use the Profile service to store parameters.
Garbin
# May 5, 2006 1:09 PM

starimpact said:

<script>
var obj=new Sys.UI.DragDropList();
</script>
I have seen the AtlasUIDragDrop.js,and DragDropList is a class,but the expression is error,why?why i can use
var timer=new Sys.Timer();
# May 7, 2006 5:29 AM

Quang Diep said:

I am study asp.net
# May 14, 2006 5:46 AM

Quang Diep said:

i am study about asp.net
# May 14, 2006 5:48 AM

Garbin said:

Quang Diep, glad to hear that! Keep studying and have fun!
Garbin
# May 14, 2006 8:04 AM

Pascal said:

Garbin,

Unfortunately, I haven't solve my problem.

I am currently trying to step thru AtlasUIDragDrop.js to figure out what is happening. So far I have tinkered with Sys.UI.DragDropList.drop, but haven't had any satisfying results.

I will also have a look at jbalderas' version of the drag and drop list and see if I can use custom events instead(http://mysite.verizon.net/vze2rrsm/atlas/CustomDragDrop/CustomDragDropList-MarchCTP.js.txt).

--Pascal
# May 16, 2006 2:27 PM

Pascal said:

Making small progress to get the copy mode working. Running into other issues. Source can be found at: http://www.geocities.com/alavanilledaycare/DNDAttempt.zip if anybody is interested (Visual Web Developer 2005 Express Edition).
Readme.txt contains some explanations.
# May 18, 2006 2:12 PM

Garbin said:

Pascal, thanks for posting this. I'll give a look at it in the next days and let you know.
Garbin
# May 18, 2006 5:30 PM

EricP said:

still can't get drag and drop copy to work - anyone have anything concrete on that?

I'm thinking I may have to capture the mouse click and copy the items myself to a div that is kept hidden - exposed when doing a DND.  

That div would be moved to the drop target - and I would just have to code manually the copying to that temporary placeholder.

Seems like I should just be able to change it from "move" to "copy" in the declaration - am I missing something or should I proceed with my workaround?
# May 19, 2006 4:29 PM

Sudheendra Kumar said:

Hi

Gr8 article!!!
I've been working on a dashboard project and this is exactly the kind of feature I wanted on my project. The only problem I have now is that every time I drag an item, the debugging trace comes into picture. How can I disable it?

Sudheendra Kumar
# May 23, 2006 8:54 AM

Garbin said:

Sudheendra, be sure that you are referencing the Release version of the Atlas files. Btw, are you using the April CTP?
# May 23, 2006 9:08 AM

Sudheendra Kumar said:

Hi

Thanks for the hint Garbin. I was infact referencing the Debug version of the Atlas files.

Just changed the 'debug="true"' line in web.config and my problem got solved.

P.S:I posted my earlier msg on Wednesday but it din't turn up here
# May 29, 2006 12:04 AM

Iman said:

Hi

How can I get the absolute position of controls after dropping?
# June 14, 2006 7:35 PM

Garbin said:

Iman, at the moment is not possible to accomplish your task because no drop event is generated when a control is dropped. The only possibility is to write custom code to do that.
# June 15, 2006 3:50 AM

koichi said:

how do you set the Debugging trace to visible false that appears right the bottom of the screen
# June 21, 2006 11:47 PM

Garbin said:

koichi, be sure to link the release version of the Atlas files (the debugging trace appears when you use the debug version).
Garbin
# June 22, 2006 3:51 AM

koichi said:

Where can i find the release version, I couldn't find it anywhere..thks in adv
# June 22, 2006 9:41 PM

Garbin said:

koichi, if you download the latest version from http://atlas.asp.net, all you have to do is add a reference to the AtlasUIDragDrop file in the ScriptManager control.
Garbin
# June 23, 2006 10:15 AM

Frank said:

I am using the floatingBehavior to drag an item from one part of the screen to another .. two quick questions..

Is there any way to monitor the position via a procedure while the floating behavior is going on?

Also, is it possible to limit the float.. i.e. the floating item can only go vertical, not horizontal?

Thanks Garbin ...
# June 29, 2006 7:28 PM

Garbin said:

Frank, I think you'll find some useful informations in my last post: http://aspadvice.com/blogs/garbin/archive/2006/06/30/18988.aspx
# July 1, 2006 7:36 AM

Frank said:

Great article on the slider .. i must be having a stupid day because I can not download the source you have posted. I joined the slackers .. when I get to your article I click on download and I get looped into the sign in screen. The page displays my name, says I am signed in as ...., where do I go to get the actual download? I assume that once I am signed in I can get the download, but there is no button to click on to get to the actual data set.


Thanks four your help!

Frank
# July 1, 2006 2:56 PM

Garbin said:

Frank, I'm sorry for that, if you can't download the code please contact me through the contact form of this blog and I'll send it to you.
Garbin
# July 1, 2006 4:02 PM

sri said:

hi i have one doupt the drag and drop i m using i got one probleem. this is the colobsibleextnder at the time before draging no proble one time i m used the drag the draging panel occuby some area. please soln required...
regards
sri
# July 11, 2006 2:35 AM

bhargava said:

hi guys
i need to dynamically remove or add a new control
what is the procedure ???
# July 13, 2006 1:50 AM

jh said:

Hi,
Can you please point me to the direction on how to remember the location of the drop targets upon the user's next visit? so that the draggables appear in the same drop targets when the user comes back the next time. Similar to Live.com's implementation.

Thanks
Jerry
# July 13, 2006 11:40 PM

Simon Hogan said:

Hi,

I am trying to add a DraggableListItem dynamically to a page. So far I can add a floating behaviour to a newly created DIV but can't get the same process to work for a draggablelistitem. Has anyone managed to sort this out, I have searched endlessly for some clue but the current doco for atlas is somewhat lacking.

The code I have so far  may help ... It will add a new listitem and makes the handle display a drag cursor, but bugs out when you try to move the new item.

The code also show a simple use of the virtualearthmap control. Most of this code has been sourced from various blogs including this one ...


Any help apprciated ...

-S-

# July 20, 2006 10:56 PM

Garbin said:

Simon Hogan: I removed your code from the comment due to bad formatting. Please check that the code is properly formatted before posting it. Thanks.
Garbin
# July 21, 2006 8:16 AM

Garbin said:

jh: the main problem is that the DragDropList doesn't raise a drop event when an item is dropped. Handling a drop event would mean being able to store the panel's location using Sys.Profile. Moreover, Sys.UI.DragDropList is a sealed class, thus cannot be extended. The solution could be to write custom code.
# July 29, 2006 4:31 AM

Edward said:

Hello,

Great example. But my question is, is there any way to use your drap and drop tool not to use the left and right area? I finally got use your tool and used it for my application and made it dynamic. Pretty cool after all said and done. Another question is there any way you can set a trigger event in situation(s) when you drag and drop it triggers a function that would either be javascript or aspx. Please if thats possible let me know.

Thanks Again
Edward
# August 9, 2006 11:31 AM

Garbin said:

Edward: the DragDropList behavior needs to be attached to a control that acts as the container for the list items, then it is up to you style the container in the appropriate way for your scenario. Unfortunately a DragDropList doesn't raise a drop event when a list item is dropped, so it's difficult to trigger a function. You could give a look to the new dragStop event raised by the DragDropManager, though I don't know if it will make easier to accomplish your task.
# August 12, 2006 2:28 PM

ATLAS Forum Posts said:

Hi, I'm trying to implement drag and drop functionality for items in a scrollable div. The dragMode must
# August 18, 2006 3:28 PM

pruiser said:

If I add "border:1px solid #000;" to the .item class, the width of any item I drag will increase a little each time I drag it. This happens on IE and Firefox. Anybody know why?
# August 20, 2006 12:36 AM

Garbin said:

pruiser: to work around this issue, add the border to another div that wraps the panel's one.
# August 20, 2006 3:43 AM

McCrown said:

Because of comment from “nonBot” with link to this domain:
The domain www.nonbot.com is expired. All Tools on this site aren’t still alive :-(
# August 20, 2006 10:50 AM

pruiser said:

Garbin: Thanks, but I'm not sure I understand. I added a div around the .item divs like this ...

<div class="itemWrapper">
   <div id="content1" class="item">
       <div id="content1Handle" class="itemHandle">Content 1</div>
       <div class="itemContent">
           <asp:Login ID="myLogin" runat="server"
               CssClass="centered"></asp:Login>
       </div>
   </div>
</div>

I also added the itemWrapper class to the style section. When the site runs, the objects can be dragged without resizing, but the borders disappear!

Is this what you meant?
# August 20, 2006 11:15 AM

pruiser said:

The wrapper should be inside the item div, wrapping the handle and the content.

           <div id="content1" class="item">
               <div class="itemWrapper">
                   <div id="content1Handle" class="itemHandle">Content 1</div>
                   <div class="itemContent">
                       <asp:Login ID="myLogin" runat="server"
                                  CssClass="centered"></asp:Login>
                   </div>
               </div>
           </div>

Thanks Garbin. Great example, btw.
# August 20, 2006 11:24 AM

McCrown said:

Although the domain www.nonbot.com expired this domain is alive again :-)

# August 21, 2006 3:51 PM

Sergio O said:

Hi, this is a great article, i like to implement an treeview control with drag and drop capabilities in it's nodes and between others treeviews, is this possible with ATLAS???..

Regards.

Sergio O.

# August 22, 2006 1:27 PM

Garbin said:

Sergio: I think it is possible though I've not tried to work with a TreeView. One thing that could help is to use a CSS adapter to make the TreeView render neat HTML.

# August 22, 2006 4:33 PM

Tanrikut said:

Hi,

I changed leftArea's orientation to horizontal and give a width to it and also to items. But it doesnt seem to be order them side by side? what is missing?

Thanks,

Tankut

# August 25, 2006 4:21 PM

Garbin said:

Tanrikut: did you try to work on the CSS for the items? They are divs so you might need to make them float.

# August 27, 2006 1:48 PM

Rabbi said:

I try to create a dynamic drop region from code

using dragdroplist and dynamically create drag item using draggablelistitem but when i wnat to drag error occur and show my dragvisual.parentNode is empty but when i use declarative syntax than no error, but why it is

occur when i want to do programatically. I see

when i declare drop region declaratively and my drag item i created from code then it works fine but when i wnat to create my drop region progamatically then error occur, So i figure out problem is to create drop region i can't solve it because declarative dragdroplist initialization is different than programatic initialization and dropcueTemplate initialization is also different i can't figure it out how i initialize dropCueTemplate. Please help me to solve my problem . I give my source code plz tell me where i make mistake and correct it.

=======================================

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dragdropProg.aspx.cs" Inherits="dragdropProg" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

   <title>Atlas Darg Page</title>

   <atlas:ScriptManager ID="scrptManager" runat="server" EnablePartialRendering="true">

   <Scripts>

      <atlas:ScriptReference ScriptName="AtlasUIDragDrop" />

   </Scripts>

   </atlas:ScriptManager>

   <link href="default.css" rel="stylesheet" type="text/css" />

</head>

<body>

   <form id="form1" runat="server">

       <div id="divContainer" style="border-right: black 1px solid; border-top: black 1px solid;

           z-index: 100; left: 89px; border-left: black 1px solid; width: 788px; border-bottom: black 1px solid;

           position: absolute; top: 134px; height: 365px">

        <div class="templates">-->

           <!-- DropCue Template -->

           <div id="dropCueTemplate" class="dropCue"></div>

               <div id="emptyTemplate" class="emptyList"> </div>

       </div>    

       </div>

   </form>

   <script type="text/javascript" language="javascript">

    var mytable;

    var leftPanel1;

    var rightPanel1;

    var middlePanel1;

   function pageLoad()

   {

     createDropZone(1,2,"50%","50%","",document.getElementById("divContainer"));

     createDragElement("Event",leftPanel1);

     createDragElement("Agent",middlePanel1);  

     createMyDropZone(middlePanel1);

     createMyDropZone(leftPanel1);

   }

     function createDragElement(controlName,whichDropRegion)

     {

         var divDrop= document.createElement("div");

         divDrop.id =controlName;

         divDrop.className="ListWindow";

         divDrop.style.height="100px";

         divDrop.style.width="300px";

         divDrop.style.position="static";

         //divDrop.style.backgroundColor="Blue";

         divDrop.style.zIndex="200";

         var dragHandle = document.createElement("div");

         dragHandle.id = divDrop.id + "_handle";

         dragHandle.style.height="25px";

         dragHandle.style.width="100%";

         dragHandle.className="itemHandle"

         //dragHandle.style.backgroundColor="Green";

         dragHandle.style.backgroundImage="url(images/bg-menu-main.png)";

         dragHandle.innerText="  "+controlName;

         dragHandle.style.fontWeight="bold";

         dragHandle.style.color="Black";

         divDrop.appendChild(dragHandle);

         whichDropRegion.appendChild(divDrop);

         addFloatingBehavior(divDrop,dragHandle);

      }

    function addFloatingBehavior(ctrl, ctrlHandle)

     {

          var dragFloatControl=new Sys.UI.Control(ctrl);

          //alert(ctrl.id);

          var dragFloatHandler=new Sys.UI.DraggableListItem();

          dragFloatControl.get_behaviors().add(dragFloatHandler);

          //t=document.getElementById(ctrl).parentNode;

          //dragFloatHandler.set_dragVisualTemplate(t.id);

          ob=new Object();

          ob="HTML";

          dragFloatHandler.set_handle(ctrlHandle);

          dragFloatHandler.set_data(ob);

          dragFloatHandler.initialize();

          dragFloatControl.initialize();

        }  

    function createMyDropZone(ctrl)

     {

           arr=new Array();

           arr.push("HTML");

           //alert("Me: "+ctrl.id);

           var dropZone = new Sys.UI.Control(ctrl);

           var dropZoneBehavior = new Sys.UI.DragDropList();

           dropZoneBehavior.set_dataType("HTML");

           dropZoneBehavior.set_acceptedDataTypes(arr);

           dropZoneBehavior.set_dragMode(Sys.UI.DragMode.Move);

           dropZoneBehavior.set_direction(Sys.UI.RepeatDirection.Vertical);

           //dropCue=new Sys.UI.DeclarativeTemplate(dropCueTemplate,dropCueTemplate.parentNode,dropCueTemplate);

           //dropCue.initialize();

          // dropZoneBehavior.set_dropCueTemplate(dropCue);

           //dropZoneBehavior.set_emptyTemplate($('emptyTemplate'));

           dropZone.get_behaviors().add(dropZoneBehavior);

           dropZoneBehavior.initialize();

           dropZone.initialize();

     }

    function createDropZone(row,column,leftDropWidth,middleDropWidth,rightDropWidth,dropRegionContainer)

    {

           //alert("I am In Drop Zone");

           mytable = document.createElement("table");

           mytable.style.position="static";

           mytable.style.height="100%";

           mytable.style.width="100%";

           mytable.border="1"

           mytablebody = document.createElement("tbody");

           for(var j = 0; j < row; j++)

           {

              mycurrent_row=document.createElement("tr");

                for(var i = 0; i < column; i++)

                {

                  mycurrent_cell = document.createElement("td");

                  mycurrent_cell.style.position="static";

                  mycurrent_cell.style.verticalAlign="top";

                  if(i==0)

                  {

                   mycurrent_cell.style.width=leftDropWidth;

                   mycurrent_cell.style.height="100%";

                   leftPanel1=document.createElement('div');

                   leftPanel1.id="leftPanel";

                   leftPanel1.style.width="100%";

                   leftPanel1.style.height="100%";

                   leftPanel1.style.background = "rgb(56,65,55)";

                   mycurrent_cell.appendChild(leftPanel1);

                  }

                   if(i==1)

                  {

                   mycurrent_cell.style.width=middleDropWidth;

                   mycurrent_cell.style.height="100%";

                   middlePanel1=document.createElement('div');

                   middlePanel1.id="middlePanel";

                   middlePanel1.style.width="100%";

                   middlePanel1.style.height="100%";

                   middlePanel1.style.background = "rgb(59,55,55)";

                   mycurrent_cell.appendChild(middlePanel1);

                  }

                   if(i==2)

                  {

                   mycurrent_cell.style.width=rightDropWidth;

                   mycurrent_cell.style.height="100%";

                   rightPanel1=document.createElement('div');

                   rightPanel1.id="rightPanel";

                   rightPanel1.style.width="100%";

                   rightPanel1.style.height="100%";

                   rightPanel1.style.background = "rgb(35,45,75)";

                   mycurrent_cell.appendChild(rightPanel1);

                  }

                  mycurrent_row.appendChild(mycurrent_cell);

                 }//end inside for loop

                 mytablebody.appendChild(mycurrent_row);

              }//end out side for loop

                mytable.appendChild(mytablebody);

                dropRegionContainer.appendChild(mytable);

           }//end function

   </script>

</body>

</html>

# August 29, 2006 2:22 AM

ryan martin said:

is it possible to drag an image into an area which would then know you want to do an action with the image you were draggin. example: i have an image gallery an someone is registered to my site an has My Photos page and in order to add photos to there page they can view photos and drag them into a designated area which would then add that photo to there photo album

# August 30, 2006 12:24 PM

Garbin said:

ryan martin: yes it is possible to do it. You could add a DataSourceDropTarget behavior to the drop zone to add the photos to a PhotoDataSource control. Check http://atlas.asp.net for some documentation about data sources. The DataSourceDropTarget behavior allows to add the data carried by the dragged object (that could be a Photo object) to a data source control.

# August 30, 2006 1:41 PM

Bob said:

Has anyone found a way to get a notification (postback preferably) telling me what object has been dropped into what target?

Thanks

Bob

# September 6, 2006 3:23 PM

Brad said:

Is there anyway to make these controls similar to webparts?

# September 7, 2006 8:54 PM

ketan said:

hi i want to add dynamic control and want to make it dragable

how can i do  this plz

reply

urgent

# September 8, 2006 5:05 AM

Garbin said:

@Bob: take a look at the DataSourceDropTarget behavior. You could try to use the event raised by the DataSource control when an item is added to its data collection... let me know if it works.

@Brad: if you need to perform most of the work on server-side, consider using WebParts. I know that there are issues with Atlas, but hopefully they'll be fixed and you'll save a lot of work.

@ketan: consider building an extender around it. You can follow the tutorial at http://atlas.asp.net/atlastoolkit/Walkthrough/CreatingNewExtender.aspx

# September 8, 2006 2:03 PM

Haresh said:

Excellent article mate! one urgent question though - I want to create the DIVs(content 1, 2 & 3) dynamically and make it drag-droppable as in your example. And from what I understand, I can only do this by adding dynamic behaviours in .aspx.CS file. Could you please tell me how can I achieve this?

Thanks :)

# September 14, 2006 2:17 PM

ATLAS Forum Posts said:

I am creating an application that uses Drag and Drop. I&#39;ve based my code from this useful example:http://aspadvice.com/blogs/garbin/archive/2006/01/17/14730.aspxThe

# September 27, 2006 4:24 PM

Aslam said:

I've had a look at Start.com which uses similar drag drop functionality and i was wondering if you knew how they got the minimise, maximise and close icons in the top right hand corner of each of their draggable controls ?

# October 4, 2006 5:41 AM

Garbin said:

@Aslam: it is part of the template for a start.com widget. I think the template is generated on client-side but nothing stops you from building it using HTML and then showing/hiding the icons based on mouse movements.

# October 5, 2006 8:07 AM

Aslam said:

Another question... I have an application that gets data from a database and then displays 2 lists each containing 10 records at a time on a page using ContentPlaceHolder.

I can create the HTML for the drop zones, drag handles, etc... dynamically when retrieveing data from the database, but how to i generate the drag drop behaviour which is displayed after the end of the </form> tag i.e.

<page>

  <components>

   .....

# October 11, 2006 8:35 AM

Garbin said:

Aslam: to generate the markup on the page you have two possibilities: first one is creating a server control that implements the IScriptControl interface and override the RenderScript() method. The second one is creating an extender. To create an extender, please check the following url:

http://ajax.asp.net/atlastoolkit/Walkthrough/CreatingNewExtender.aspx

# October 11, 2006 2:02 PM

ATLAS Forum Posts said:

I have a master page and within the ContentPlaceHolder I want to 2 lists containingup to 10 namesper

# October 11, 2006 2:07 PM

Aslam said:

Garbin: Thanks for the info... you may have noticed that i put a posting on the ASP.NET ATLAS forums... anyway, I have tried your link but it seems you need VS 2005 and I only have C# Express and VWD so I cannot run through the tutorial ...

# October 12, 2006 6:10 AM

The Imaginative Universal said:

# October 13, 2006 10:05 AM

The Imaginative Universal said:

# October 17, 2006 10:15 AM

vu nguyen said:

How can I remove debuging trace box that it appear bottom

# October 18, 2006 11:58 PM

john mcfetridge said:

Maybe I am expecting too much from MS Ajax drag/drop support but it seems pretty limited and hope I am missing a key point that someone can enlighten me on. I have included a simple

Scriptaculous drag/drop example that shows how easy it is drag/drop between any HTML elements on a page. I need this kind of functionality in developing a Rich Internet App so for example a user can select a name from a list and drop it on a cell in a table (grid). I spent a day and half trying to do this in MS Ajax and failed. Either I cannot find the right Docs which are scarce or the function is not there (rearranging divs does not cut it). It took my 2 hours to do this example in scriptaculous as I had never worked with it! All my searching on the web turns up the answer that MS Ajax does not fire drop events. Surely this is not the case for I do not want to lug around multiple script libraries and that the same thing can be done easily in MS Ajax

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

   <title>Untitled Page</title>

   <script src="script/prototype.js" type="text/javascript"></script>

    <script src="script/scriptaculous.js" type="text/javascript"></script>

    <script src="script/effects.js" type="text/javascript"></script>

     <script src="script/dragdrop.js" type="text/javascript"></script>

 <script type="text/javascript">

 //add element to the droppable targets note the anonoymous function used as a event handler which

 //in this case just changes the contents of the target text to the source element's

  function AddToDropTargets( id)

    {

        Droppables.add(id, {accept:null,onDrop: function(sourceelement,targetelement)

        {

           targetelement.innerHTML = sourceelement.innerHTML;

         }

       });

    }

   function dragstuff()

   {

     if (!Draggable)

     {

       alert("libraries did not load");

       return;

     }

     //define draggable element

     new Draggable(l1,{revert:true});

     //define possible drop targets

     AddToDropTargets('cell1');

     AddToDropTargets('cell2');

     AddToDropTargets('cell3');

     }

 </script>

</head>

<body onload="dragstuff()">

<label id="l1" style=" border: 1px solid black ">drag me</label>

<br />

Drop Targets

<table>

 <tr>

   <td>

   <label id="cell1"  >DropTarget1</label>

   </td>

   <td>

   <label id="cell2" >DropTarget2</label>

   </td>

   <td>

   <label id="cell3" >DropTarget3</label>

   </td>

 </tr>

 <tr>

   <td>

   <label id="Label1"  >normal</label>

   </td>

   <td>

   <label id="Label4"  >normal</label>

   </td>

   <td>

   <label id="Label5"  >normal</label>

   </td>

 </tr>

</table>

</body>

</html>

# October 20, 2006 10:21 AM

Chris Dodge said:

Can anyone confirm/deny Johns above assertion that Atlas does not fire "OnDrop" events? This is essential for my application. While it is nice to have drag/drop for layout changes, it's not sufficient for building rich client-side apps...

# November 7, 2006 11:55 AM

Garbin said:

Chris: this shouldn't be a problem starting from beta1, since sealed class are removed and with the prototype model every method is virtual. Therefore, you could inherit from every class, call the base methods and add your events.

# November 7, 2006 12:04 PM

Aslam said:

Hi... I asked on 11 Oct 2006 about the following....

"I can create the HTML for the drop zones, drag handles, etc... dynamically when retrieveing data from the database, but how to i generate the drag drop behaviour which is displayed after the end of the </form> tag i.e.

<page>

 <components>

  .....

"

Garbin mentioned that I can use use a control extender. I have watched the video on creating a cusotm extender at:

http://www.asp.net/learn/videos/default.aspx?tabid=63

and also tried to follow the example above with no luck. Can anyone explain in detail how to create the script (see below) dynamically as you create controls dynamically ?

<script type="text/xml-script">

       <page>

           <components>

               <!-- Left Area -->

               <control id="leftArea">

                   <behaviors>

Thanks

# November 13, 2006 10:57 AM

Aslam said:

I have tried to cheat and have manually copied the <script type="text/xml-script">

      <page>

          <components>

              <!-- Left Area -->

              <control id="leftArea">

                  <behaviors>

etc... and pasted it into the Master page... BUT I get the following error message everytime a page loads - I assume this is because the elements have not yet been rendered on the page and I am trying to reference them ?

# November 14, 2006 3:43 AM

Aslam said:

Sorry ... error message reads:

Assertion Failed: Could not find an HTML element with ID "areaLeft" for control of type "Sys.UI.Control"

# November 14, 2006 3:51 AM

ASP.NET AJAX Forum Posts said:

I am trying to get drag and drop to work with data dynamically populated from a database. The basic structure

# November 14, 2006 9:22 AM

Aslam said:

Garbin... do you have any leads/suggestions on the above ? Is it possible ? I have not seen any worked examples on the net ...

# November 16, 2006 10:48 AM

Sainath said:

Hi,

  How can i fetch the data from database say oracle, bind it to the Listview control, and the drag and drop elements in the list view.

# November 17, 2006 4:00 AM

The Imaginative Universal said:

# November 17, 2006 12:14 PM

The Imaginative Universal said:

Being able to drag html elements around a page and have them stay where you leav

# November 20, 2006 10:00 AM

Milind said:

Can you please update the code to support ajax framework Nov CTP? I tried to update this without luck. The java script error "Array.add is not a function" is shown.

# November 21, 2006 5:13 PM

sainath said:

Hi Garbin,

  When i drag a checkbox which is checked and on droping the checkbox it is unchecked.can you provide solution for this.

# November 22, 2006 8:55 AM

k said:

how to do it in java

# December 20, 2006 10:02 AM

U3 said:

Dear ,,

what a nice sample , Good WORk man,

but how about extendable panel inside the div and alos it can be draggable..for example :

using webpanel of infragistics , it work well, but i want to close this panle for example , it doesnt work..please provide me with a solution

# December 21, 2006 7:20 AM

ASP.NET AJAX Forum Posts said:

Hello, Back in Atlas Time (6 monthes ago), I used Garbin's great exemple for a drag and drop found here:

# January 3, 2007 12:03 PM

Garbin said:

You can find an updated version of the code here:

http://forums.asp.net/1516781/ShowThread.aspx#1516781

# January 3, 2007 12:42 PM

julie said:

Could u plz guide me how to implement drag and drop in treeview in asp.net 2.0?

# January 15, 2007 1:46 AM

Qcsea said:

Dynamic create(Use literal control):

Literal1.Text = "<div id=\"content2\" class=\"item\"><div id=\"content2Handle\" class=\"itemHandle\">Content 2</div><div class=\"itemContent\">TestTest</div></div>";

       Literal2.Text = "<script type=\"text/xml-script\">        <page>            <components>                                <!-- Left Area -->                <control id=\"leftArea\">                    <behaviors>                        <dragDropList dragDataType=\"HTML\"                                      acceptedDataTypes=\"'HTML'\"                                      dragMode=\"Move\"                                      direction=\"Vertical\">                            <dropCueTemplate>                                <template layoutElement=\"dropCueTemplate\" />                            </dropCueTemplate>                            <emptyTemplate>                                <template layoutElement=\"emptyTemplate\" />                            </emptyTemplate>                        </dragDropList>                    </behaviors>                </control>                                <!-- Right Area -->                <control id=\"rightArea\">                    <behaviors>                        <dragDropList dragDataType=\"HTML\"                                      acceptedDataTypes=\"'HTML'\"                                      dragMode=\"Move\"                                      direction=\"Vertical\">                            <dropCueTemplate>                                <template layoutElement=\"dropCueTemplate\" />                            </dropCueTemplate>                            <emptyTemplate>                                <template layoutElement=\"emptyTemplate\" />                            </emptyTemplate>                        </dragDropList>                    </behaviors>                </control>                                <!-- Draggable items -->                <control id=\"content1\">                    <behaviors>                        <draggableListItem handle=\"content1Handle\" />                    </behaviors>                </control>                <control id=\"content2\">                    <behaviors>                        <draggableListItem handle=\"content2Handle\" />                    </behaviors>                </control>                <control id=\"content3\">                    <behaviors>                        <draggableListItem handle=\"content3Handle\" />                    </behaviors>                </control>                            </components>        </page>    </script>";

# January 29, 2007 11:58 AM

Kart said:

The code in this URL works perfectly ! http://forums.asp.net/1516781/ShowThread.aspx#1516781

I am interested in knowing how to save those drop target positions. Store them in Profile ?

Thanks

# February 22, 2007 10:55 AM

dynaclips said:

The updated code works great.  Thanks!

It works in the Content area using master pages.

It works inside a Multiview in the Content area using master pages.

However, it does not work if it is inside a Multiview in the Content area IF that contentPlaceHolder is wrapped with an UpdatePanel on the Master page.

Removing the Update panel from the master page solves the problem.

# March 3, 2007 12:16 PM

Arnout said:

# March 20, 2007 6:30 PM

UstesG said:

The Literal works great for xml script.  Did anybody ever figure out how to get the drop event?

# October 25, 2007 12:52 AM

megasoft78 said:

# April 23, 2008 3:10 PM

Garbin said:

Nice article! Read and voted :)

# May 15, 2008 6:58 AM
New Comments to this post are disabled