Drag / Drop events



Adding nodes to a treeview instance is easy - just create a new JQTreeNode instance for each new, set its Text property and add it to the Nodes collection of the treeview instance. You can add unlimited levels of nested nodes at once - each node has its own Nodes collection of child nodes.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="JQSuiteASPNETExample.examples.tree.draganddrop._default" %>
<%@ Register Assembly="Trirand.Web" TagPrefix="trirand" Namespace="Trirand.Web.UI.WebControls" %>

<!DOCTYPE html>
<html lang="en">
<head id="Head1" runat="server">
    <meta charset="utf-8">
    <title>jqTreeView for ASP.NET WebForms - Drag And Drop</title>
    <!-- The jQuery UI theme that will be used by the treeview  -->
    <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.10.3/themes/redmond/jquery-ui.css" />
    <!-- The jQuery UI theme extension jqTreeView needs -->
    <link rel="stylesheet" type="text/css" media="screen" href="/themes/ui.jqtreeview.css" />
    <!-- jQuery runtime minified -->
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-2.0.3.min.js" type="text/javascript"></script>    
    <!-- The jqTreeView client-side javascript -->
    <script src="/js/trirand/jquery.jqTreeView.min.js" type="text/javascript"></script>    
    <style type="text/css">    
        body, html { font-size: 80%; }    
    </style>
</head>

<body>
    <form id="form1" runat="server">
    <table style="height:350">
        <tr>
            <td>
                <trirand:JQTreeView runat="server" 
                        ID="JQTreeView1" 
                        Width="200" 
                        Height="400"
                        MultipleSelect="true"                        
                        DragAndDrop="true"
                        onnodesdropped="JQTreeView1_NodesDropped">
                    <Nodes>
                        <trirand:JQTreeNode Text="North America" Expanded="true" ImageUrl="/images/folder.png" ExpandedImageUrl="/images/folder-open.png">
                            <Nodes>
                                <trirand:JQTreeNode Text="USA" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Mexico" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Canada" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                            </Nodes>
                        </trirand:JQTreeNode>
                        <trirand:JQTreeNode Text="South America" Expanded="true" ImageUrl="/images/folder.png" ExpandedImageUrl="/images/folder-open.png">
                            <Nodes>
                                <trirand:JQTreeNode Text="Brazil" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Argentina" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Chile" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Costa Rica" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Columbia" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                            </Nodes>
                        </trirand:JQTreeNode>
                        <trirand:JQTreeNode Text="Europe" Expanded="true" ImageUrl="/images/folder.png" ExpandedImageUrl="/images/folder-open.png">
                            <Nodes>
                                <trirand:JQTreeNode Text="England" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Germany" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Norway" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Sweden" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Italy" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="France" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                            </Nodes>
                        </trirand:JQTreeNode>
                    </Nodes> 
                    <ClientSideEvents NodesDragged="nodesDragged" NodesDropped="nodesDropped" />                    
                </trirand:JQTreeView>
            </td>
            <td>
                 <trirand:JQTreeView runat="server" 
                        ID="JQTreeView2" 
                        Width="200" 
                        Height="400"
                        MultipleSelect="true"
                        DragAndDrop="true" onnodesdropped="JQTreeView2_NodesDropped">
                    <Nodes>
                        <trirand:JQTreeNode Text="North America" Expanded="true" ImageUrl="/images/folder.png" ExpandedImageUrl="/images/folder-open.png">
                            <Nodes>
                                <trirand:JQTreeNode Text="USA" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Mexico" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Canada" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                            </Nodes>
                        </trirand:JQTreeNode>
                        <trirand:JQTreeNode Text="South America" Expanded="true" ImageUrl="/images/folder.png" ExpandedImageUrl="/images/folder-open.png">
                            <Nodes>
                                <trirand:JQTreeNode Text="Brazil" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Argentina" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Chile" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Costa Rica" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Columbia" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                            </Nodes>
                        </trirand:JQTreeNode>
                        <trirand:JQTreeNode Text="Europe" Expanded="true" ImageUrl="/images/folder.png" ExpandedImageUrl="/images/folder-open.png">
                            <Nodes>
                                <trirand:JQTreeNode Text="England" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Germany" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Norway" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Sweden" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="Italy" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                                <trirand:JQTreeNode Text="France" ImageUrl="/images/document.png"></trirand:JQTreeNode>
                            </Nodes>
                        </trirand:JQTreeNode>
                    </Nodes>
                    <ClientSideEvents NodesDragged="nodesDragged" NodesDropped="nodesDropped" />
                </trirand:JQTreeView>            
            </td>
            <td width="200" valign="top">                 
                <fieldset style="font-size 10pt; font-family: Verdana;">
                    <legend>Drag / Drop events</legend>
                    <div id="dragDropLog" style="height:300px; overflow: auto;"></span>
                </fieldset>
            </td>
            </td>
        </tr>
    </table>
    
     <script type="text/javascript">    
    
    function nodesDragged(args, event) {
        // NodesDragged client-side event has the following arguments
        // args.draggedNodes -> array, list of nodes being currently dragged
        // args.sourceTreeView -> an instance to the client-side object of the parent treeview
        // event -> the browser event object
        
        var srcTreeView = args.sourceTreeView; // the instance to the treeview parent                
        
        var draggedNodesText = "";
        for (var i=0; i<args.draggedNodes.length; i++) {
            var draggedNodeOptions = srcTreeView.getNodeOptions(args.draggedNodes[i]); // get node options like Text, Value, etc    
            draggedNodesText = draggedNodesText + draggedNodeOptions.text + ",";
        }           
        
        
        var message = "<b>" + draggedNodesText + "</b>" + 
                      " dragged from " + srcTreeView.options.id;                      
        
        var dragDropLog = $("#dragDropLog");
        dragDropLog.prepend(message);
    };
    
    function nodesDropped(args, event) {
        // NodesDropped client-side event has the following arguments
        // args.draggedNodes -> array, list of nodes being currently dragged
        // args.sourceTreeView -> an instance to the client-side object of the parent treeview
        // args.destinationNode -> the reference to the destination node
        // args.destinationTreeView -> the reference to the destination treeview
        
        var srcTreeView = args.sourceTreeView; // the instance to the treeview parent        
        var destTreeView = args.destinationTreeView; // get the destination treeview
        var destNode = args.destinationNode; // destination node can only be one, no need for array 
        
        var draggedNodesText = "";
        for (var i=0; i<args.draggedNodes.length; i++) {
            var draggedNodeOptions = srcTreeView.getNodeOptions(args.draggedNodes[i]); // get node options like Text, Value, etc    
            draggedNodesText = draggedNodesText + draggedNodeOptions.text + ",";
        }   
        
        var destNodeOptions = destTreeView.getNodeOptions(destNode); // get the options of the destination node
        
        var message = "<b>" + draggedNodesText + "</b>" + 
                      " from " + srcTreeView.options.id + " dropped on " +
                      "<b>" + destNodeOptions.text + "</b>" +
                      " from " + destTreeView.options.id + "<br/>";
        
        var dragDropLog = $("#dragDropLog");
        dragDropLog.prepend(message);
    };
    
    </script>
        
    <br />
    <br />
    <br />
    <trirand:codetabs runat="server" ID="tabs"></trirand:codetabs>
    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Trirand.Web.UI.WebControls;

namespace JQSuiteASPNETExample.examples.tree.draganddrop
{
    public partial class _default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void JQTreeView1_NodesDropped(object sender, Trirand.Web.UI.WebControls.JQTreeViewNodesDropppedEventArgs e)
        {
            // you can get the source and destination treeviews and
            // nodes using the event arguments of the NodesDropped event handler
            // you can use this information to update database, file system, etc
            // this method will be called via AJAX Get request - just a ping
            // back to the server, so we can update information if needed
            // but the page will not refresh and you will not be able to update
            // other server control from here
            string sourceTreeViewID = e.SourceTreeViewID;
            string destTreeViewID = e.DestinationTreeViewID;
            List draggedNodes = e.DraggedNodes;
            JQTreeNode destNode = e.DestinationNode;
        }

        protected void JQTreeView2_NodesDropped(object sender, Trirand.Web.UI.WebControls.JQTreeViewNodesDropppedEventArgs e)
        {

        }
    }
}
Switch theme:


Theming is based on the very popular jQuery ThemeRoller standard. This is the same theming mechanism used by jQuery UI and is now a de-facto standard for jQuery based components. The benefits of using ThemeRoller are huge. We can offer a big set of ready to use themes created by professional designers, including Windows-like themes (Redmond), Apple-like theme (Cupertino), etc. In addition to that any jQuery UI controls on the same page will pick the same theme.

Last, but not least, you can always roll your own ThemeRoller theme, using the superb Theme Editor

To use a theme, simply reference 2 Css files in your Html <head> section - the ThemeRoller theme you wish to use, and the jqGrid own ThemeRoller Css file. For example (Redmond theme):

<link rel="stylesheet" type="text/css" media="screen" href="/themes/redmond/jquery-ui-1.8.2.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="/themes/ui.jqgrid.css" />