In this article I’m going to explain how to make expandable treeview node in C# when others node is closed. Here I’m just creating a method to perform this task with name TreeView1_TreeNodeExpanded. Let’s take a look of this method code:

Foreground code:

<asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" OnTreeNodeExpanded="TreeView1_TreeNodeExpanded" ShowLines="True">

Code-behind:
protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
         {
              TreeNodeCollection ts = null;
             if (e.Node.Parent == null)
             {
                 ts = ((TreeView)sender).Nodes;
             }
             else
                 ts = e.Node.Parent.ChildNodes;
             foreach (TreeNode node in ts)
             {
                 if (node != e.Node)
                 {
                     node.Collapse();
                 }
             }
         }

  Thanks for shwing your interest in this article.


  Modified On Nov-16-2017 07:59:27 PM
  1. Hi zhang wei,
    Welcome to our MindStick Developers Section. Here you can share your knowledge to help others.
    You cannot upload same content in more than one section, if its done then only one of them will be approved.
    Looking forward for more Articles/Blogs from your side.
    Thanks.
  1. Hi Haider M Rzvi,thank you for help me,my english not well ,I belive i can to do more better!

    I very Thanks!

Leave Comment