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

Reload RadTreeView and select specific value

1 Answer 275 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 16 Feb 2010, 02:43 PM
Hi,
       I wish to load a RadTreeView, which will populate form fields and then allow editing of these form fields. When the changes are made to the form field, we then will save the details away and re-load the tree with the new values. However I can't get the tree to appear in the same position as it was before updating, with the same node selected. How can I get to the required node and traverse to this node.

As you can see from my code I'm trying to get the text from the selected node before it is updated and then try and go back to this node, however the tree just shows the root nodes, with nothing expanded.       
Thanks in advance.


         if (Page.IsValid)
        {
            string SelectedText = RadTreeView1.SelectedNode.Text ;
            RadTreeNode MyNode = RadTreeView1.SelectedNode.ParentNode;

            //Get the parent node of the changed node
            if (MyNode != null)
            {
                while (MyNode.ParentNode != null)
                {

                    MyNode = MyNode.ParentNode;

                }
            }
            else
            {
                MyNode = RadTreeView1.SelectedNode;

            }


           // Change the underlying code
            Servizio.ServizioBase servizio = new Servizio.ServizioBase();
            rms.Category category = new rms.Category();
            category.Get(Convert.ToInt32(this.txt_id.Text));
            category.CategoryNo = Convert.ToDecimal (txt_CategoryNo.Text);
         
            category.save();

            // Reload the grid
            RadTreeView1.Nodes.Clear();
            LoadRootNodes(this.RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);

             // find the node containing the value that was changed
            RadTreeView1.Nodes.FindNodeByText(MyNode.Text).ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
            RadTreeView1.Nodes.FindNodeByText(MyNode.Text).ExpandChildNodes();


1 Answer, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 16 Feb 2010, 04:01 PM
This setup (albeit a bit simple) works for me:

ASPX:
<asp:Button ID="myButton" runat="server" Text="Test" OnClick="myButton_Click" /> 
    <br /> 
    <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="Test"
                                <Nodes> 
                                    <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 1"
                                    </telerik:RadTreeNode> 
                                    <telerik:RadTreeNode runat="server" Text="Child RadTreeNode 2"
                                    </telerik:RadTreeNode> 
                                </Nodes> 
                            </telerik:RadTreeNode> 
                        </Nodes> 
                    </telerik:RadTreeNode> 
                </Nodes> 
            </telerik:RadTreeNode> 
            <telerik:RadTreeNode runat="server" Text="Root RadTreeNode2"
            </telerik:RadTreeNode> 
            <telerik:RadTreeNode runat="server" Text="Root RadTreeNode3"
            </telerik:RadTreeNode> 
        </Nodes> 
    </telerik:RadTreeView> 

C#:
    protected void myButton_Click(object sender, EventArgs e) 
    { 
        RadTreeNode myNode = RadTreeView1.FindNodeByText("Test"); 
        myNode.ExpandMode = TreeNodeExpandMode.ServerSide; 
        myNode.Expanded = true
        myNode.ExpandParentNodes(); 
        myNode.ExpandChildNodes(); 
        myNode.Selected = true
    } 

Looking through your code I notice that you never actually set the expanded property of the node you are looking for and you call RadTreeView1.Nodes.FindNodeByText as opposed to RadTreeView1.FindNodeByText. I also call the ExpandParentNodes() as well as set the selected property to true. This is all done in an OnClick event but the same logic should be applicable to your situation.
Tags
TreeView
Asked by
Mark
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Share this question
or