This is a migrated thread and some comments may be shown as answers.

Moving nodes up/down server-side

4 Answers 182 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Craig
Top achievements
Rank 1
Craig asked on 29 Nov 2010, 09:57 PM
Hello,

Can you provide a server-side code sample that shows how to programatically move the selected node up or down in a treeview?

Thanks, Craig

4 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 30 Nov 2010, 11:40 AM
Hello Craig,


With RadTreeView it is easy to add, remove or disable nodes at runtime. The demo shows how to add, remove a node upon NodeClick.
Demo : Add/Remove/Disable Nodes


I believe you can make use of same technique for your requirement. First get the SelectedNode in code behind and create a new node. Now add the new node using Insert() method to node collection accordingly (after getting SelectedNode.Index). And delete the SelectedNode using RemoveAt() method.



-Shinu.
0
Craig
Top achievements
Rank 1
answered on 01 Dec 2010, 01:36 AM
Thanks very much, Shinu. That works fine to move nodes that are not parents. However, when the selected node to be moved is a parent, in addition to the new node being created/cloned, all of its child nodes need to be cloned as well. And if those child nodes are parents, then the copying must continue further probably leading to a recursive solution. The situation is further complicated since I amd databinding the nodes so I can display different data field values in the template contriols I am using for each node. Because of this, I found that I have to rebind the nodes when I clone them.

Anyway, I assume the drag and drop functionality in the tree control must do similar manipluations to be able to move parents and their associated child nodes efficiently. Perhaps that could provide some insight into the best way to approach a solution.  We are currently evaluating the suite for possible purchase soon, so any assistance you can provide would be greatly appreciated.

Thanks, Craig

0
Accepted
Schlurk
Top achievements
Rank 2
answered on 01 Dec 2010, 10:35 PM
If you move nodes around they should still retain all of the descendant nodes. So if you get an instance of Node A and it has Child Node A & Child Node B that particular instance of Node A should still have its nodes in a node collection. So for example:

ASPX:
<asp:Button ID="TestButton" runat="server" Text="Test" OnClick="TestButton_Click" />
 
<div>
    <telerik:RadTreeView ID="RadTreeView1" runat="server">
        <Nodes>
            <telerik:RadTreeNode runat="server" Text="Root RadTreeNode1">
                <Nodes>
                    <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 1">
                    </telerik:RadTreeNode>
                    <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 2">
                        <Nodes>
                            <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 1">
                            </telerik:RadTreeNode>
                            <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 2">
                            </telerik:RadTreeNode>
                            <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 3">
                            </telerik:RadTreeNode>
                        </Nodes>
                    </telerik:RadTreeNode>
                    <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 3">
                    </telerik:RadTreeNode>
                </Nodes>
            </telerik:RadTreeNode>
            <telerik:RadTreeNode runat="server" Text="Root RadTreeNode2">
            </telerik:RadTreeNode>
            <telerik:RadTreeNode runat="server" Text="Root RadTreeNode3">
            </telerik:RadTreeNode>
            <telerik:RadTreeNode runat="server" Text="Root RadTreeNode4">
            </telerik:RadTreeNode>
            <telerik:RadTreeNode runat="server" Text="Root RadTreeNode5">
            </telerik:RadTreeNode>
            <telerik:RadTreeNode runat="server" Text="Root RadTreeNode6">
            </telerik:RadTreeNode>
        </Nodes>
    </telerik:RadTreeView>

C#:
protected void TestButton_Click(object sender, EventArgs e)
{
    RadTreeNode myNode = RadTreeView1.Nodes[0];
    RadTreeView1.Nodes.Remove(myNode);
    RadTreeView1.Nodes.Insert(5, myNode);
}


As you can see from running this sample, Root RadTreeNode1 is now on the bottom of the RadTreeView with all of its nodes still intact. They are contained in the RadTreeNode's Node collection, which you can access particular nodes from via index, value, text etc.

Hope this helps! :)
0
Craig
Top achievements
Rank 1
answered on 01 Dec 2010, 11:18 PM
That works perfect. Thanks very much.

Craig
Tags
TreeView
Asked by
Craig
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Craig
Top achievements
Rank 1
Schlurk
Top achievements
Rank 2
Share this question
or