Moving nodes up/down server-side

5 posts, 1 answers
  1. Craig
    Craig avatar
    12 posts
    Member since:
    Nov 2010

    Posted 29 Nov 2010 Link to this post

    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
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 30 Nov 2010 Link to this post

    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.
  3. Craig
    Craig avatar
    12 posts
    Member since:
    Nov 2010

    Posted 30 Nov 2010 Link to this post

    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

  4. Answer
    Schlurk
    Schlurk avatar
    812 posts
    Member since:
    May 2009

    Posted 01 Dec 2010 Link to this post

    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! :)
  5. Craig
    Craig avatar
    12 posts
    Member since:
    Nov 2010

    Posted 01 Dec 2010 Link to this post

    That works perfect. Thanks very much.

    Craig
Back to Top