Expand TreeNode at Server Side

2 posts, 1 answers
  1. Phaneendra
    Phaneendra avatar
    27 posts
    Member since:
    Jul 2011

    Posted 28 Apr 2012 Link to this post

    Hi,
    In my aspx page, left side I'm displaying Treeview, right side a Panel which nodes for creating new node.
    I'm Creating Treeview dynamically.
    I have a Treeview context menu for creating new node.
    When user creates new node using context menu(user provides details on right side), and clicks on create, that nodes should be created and that node path to be expanded and selected.
    I 've tried like this

    string NewNodeName = ViewState["NewChart"] != null ? ViewState["NewChart"].ToString() : string.Empty;
               foreach (RadTreeNode node in TreeLubeChart.GetAllNodes())
               {
                   if (node.Text == NewNodeName)
                   {
                       node.Selected = true;
                       node.Expanded = true;
                   }
               }
    But this is not making my work done. I placed this under button click where new node is created, but New node which user provides is creating, but I need its path to be expanded and selected state.


    Thanks
    Krishna
  2. Answer
    Plamen
    Admin
    Plamen avatar
    2729 posts

    Posted 02 May 2012 Link to this post

    Hi Phaneedra,

     
    You can refer to this online demo and add the following code in yellow that expands all the parent nodes when a node is added:

    protected void AddButton_Click(object sender, EventArgs e)
            {
                Page.Validate("TextRequired");
                if (Page.IsValid)
                {
                    IRadTreeNodeContainer target = RadTreeView1;
                    if (RadTreeView1.SelectedNode != null)
                    {
                        RadTreeView1.SelectedNode.Expanded = true;
                        target = RadTreeView1.SelectedNode;
                    }
                         
     
                    RadTreeNode addedNode = new RadTreeNode(NodeTextBox.Text);
                    addedNode.Selected = true;
                    target.Nodes.Add(addedNode);
     
                    RadTreeNode parentNode = addedNode.ParentNode;
                    while (parentNode != null)
                    {
                        parentNode.Expanded=true;
                        parentNode = parentNode.ParentNode;
                    }
     
                }
            }

    Hope this will help you.


    Kind regards,
    Plamen Zdravkov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top