Dragging and dropping with ASP.NET 2.0 and Atlas



Introduction

This tutorial is intended to help readers understand how certain aspects of Microsoft's new AJAX Extensions technology works. AJAX Extensions is intended to simplify the development of AJAX-style functionality. As with all technologies, however, to use a tool well, it is important to understand the underlying technology that Atlas abstracts. One of the key ASP.NET AJAX abstractions is the new XML markup syntax developed to make coding with AJAX easier (originally included with the core Atlas files, XML markup is now a part of the CTP called AJAX Futures). With XML markup, developers can modify their code declaratively. However, there are times when a developer may want to be able to change her code programmatically, and in order to accomplish this, she will need to understand that underneath the markup abstraction, she is actually dealing with good 'ol JavaScript and some custom JavaScript libraries developed by Microsoft. In order to demonstrate the relationship between the Atlas declarative model and the programmatic model, I will go through a series of examples in which the same task will be accomplished both declaratively and programmatically. I will be demonstrating how to use the PreviewDragDrop library file to perform basic drag-drop operations as well as setting up drop zones.


Background

As I write this, Microsoft has made some important changes to ASP.NET AJAX for the Beta 2 that have the unfortunate side-effect of breaking most of the original Atlas implementation, and has required a bit of rework of the original samples. These revised examples apply to the Beta 2 of ASP.NET AJAX. Future releases of AJAX Extensions may affect the accuracy of this tutorial. I will attempt to update the code as new versions of AJAX Extensions become available. AJAX Extensions works with .NET 2.0, and will work with Orcas when it is released.

I. Declarative Drag Drop

The first task is to use XML markup to add drag-drop behavior to a div tag. By drag and drop, I just mean the ability to drag an object and then have it stay wherever you place it. The more complicated behavior of making an object actually do something when it is dropped on a specified drop target will be addressed later in this tutorial. To configure your webpage to use ASP.NET AJAX, you will need to install the Microsoft.Web.Extensions.dll into your Global Assembly Cache. You will also need a reference to the library Microsoft.Web.Preview.dll. Finally, you will need to configure your web.config file with the following entry:

<system.web>
    <pages>
        <controls>
            <add tagPrefix="asp" namespace="Microsoft.Web.UI" 
                 assembly="Microsoft.Web.Extensions, Version=1.0.61025.0, 
                 Culture=neutral,  PublicKeyToken=31bf3856ad364e35" />
            <add tagPrefix="asp" namespace="Microsoft.Web.UI.Controls" 
                 assembly="Microsoft.Web.Extensions, Version=1.0.61025.0, 
                 Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
            <add tagPrefix="asp" namespace="Microsoft.Web.Preview.UI" 
                 assembly="Microsoft.Web.Preview" />
        </controls>
    </pages>
</system.web>

You will need to add an Atlas Script Manager control to your .aspx page and configure it to use the PreviewDragDrop library file:

<asp:ScriptManager ID="ScriptManager1" runat="server">
    <Scripts>
        <asp:ScriptReference 
             Name="Microsoft.Web.Resources.ScriptLibrary.PreviewScript.js" />
 <asp:ScriptReference 
      Name="Microsoft.Web.Resources.ScriptLibrary.PreviewDragDrop.js" />
    </Scripts>
</asp:ScriptManager>
 

Add the div object you want to make draggable, and make sure it has a drag handle:

<div style="background-color:Red;height:800px;width:600px;">
    <div id="draggableDiv" 
         style="height:100px;width:100px;background-color:Blue;">
        <div id="handleBar" 
             style="height:20px;width:auto;background-color:Green;">
        </div>
    </div>
</div>
 

Finally, add the markup script that will make your div draggable:
<script type="text/xml-script">
    "http://schemas.microsoft.com/xml-script/2005">
        
            "draggableDiv">
                
                    "handleBar"/>
                behaviors>
            </control>
        </components>
    </page>
</script>
 

And with that, you should have a draggable div
tag. The example demonstrates the simplicity and ease of using the
declarative model with AJAX Extensions. In the terminology being
introduced with AJAX Futures, you have just used declarative markup to
add floating behavior to an HTML element.

 

II. Imperative Drag Drop

To accomplish the same thing using a programmatic model requires a bit more code, but not much more. It is important to understand that when you add an AJAX Extensions Script Manager component to your page, you are actually giving instructions to have the AJAX Extensions JavaScript library loaded into your page. The AJAX Extensions library, among other things, provides client-side classes that extend the DOM and provide you with tools that allow you to code in a browser agnostic manner (though there currently are still issues with Safari compatibility). These client-side classes also allow you to add behaviors to your HTML elements.
To switch to an imperative model, you will need to replace the XML markup with two JavaScript functions. The first one is a generic script to add floating behavior to an HTML element. It leverages the AJAX Extensions client-side classes to accomplish this:

function createDraggableDiv() {
    var panel= document.createElement("div");
    panel.style.height="100px";
    panel.style.width="100px";
    panel.style.backgroundColor="Blue";
    var panelHandle = document.createElement("div");
    panelHandle.style.height="20px";
    panelHandle.style.width="auto";
    panelHandle.style.backgroundColor="Green";
    panel.appendChild(panelHandle);
    var target = $get('containerDiv').appendChild(panel);
    addFloatingBehavior(panel, panelHandle);
}

You will then just need to add a button to the page that calls the "createDraggableDiv()" function. The new HTML body should look something like this:

<input type="button" value="Add Floating Div" onclick="createDraggableDiv();" />
<div id="containerDiv" style="background-color:Purple;height:800px;width:600px;"/>
 

This will allow you to add as many draggable elements to your page as
you like, thus demonstrating the power and flexibility available to you
once you understand the relationship between using AJAX Extensions
declaratively and using it programmatically. As a point of reference,
here is the complete code for the dynamic drag and drop example:
 
<%@ 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 runat="server">
<title>Imperative Drag and Drop II</title>
<script type="text/javascript">
        
        function createDraggableDiv() {
             var panel = document.createElement("div");
             panel.style.height="100px";
             panel.style.width="100px";
             panel.style.backgroundColor="Blue";
             var panelHandle = document.createElement("div");
             panelHandle.style.height="20px";
             panelHandle.style.width="auto";
             panelHandle.style.backgroundColor="Green";
             panel.appendChild(panelHandle);
             var target = $get('containerDiv').appendChild(panel);
             addFloatingBehavior(panel, panelHandle);
             }
            
            function addFloatingBehavior(ctrl, ctrlHandle){
                 $create(Sys.Preview.UI.FloatingBehavior,
                {'handle':
                    ctrlHandle}, null, 
                        null, ctrl);
                     }

script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
        <asp:ScriptReference 
           Name="Microsoft.Web.Resources.ScriptLibrary.PreviewScript.js"
/>
 <asp:ScriptReference 
   Name="Microsoft.Web.Resources.ScriptLibrary.PreviewDragDrop.js"
/>
</Scripts>
</asp:ScriptManager>
<h2>Imperative Drag and Drop Code with javascript: 
         demonstrate dynamic loading of behaviors</h2>
<input type="button" value="Add Floating Div" 
          onclick="createDraggableDiv();"
/>
<div id="containerDiv" 
   style="background-color:Purple;height:800px;width:600px;"/>
</form>
</body>
</html> 

0 comments: