ASP.NET TreeView control is a server side control for displaying data in a hierarchical formats. TreeView contains multiple nodes and sub nodes. Many times we bind a large number of data with tree view for users to navigate data, pages, or displaying any other details information.
If there are many nodes and sub nodes in TreeView, sometime it’s quite difficult for a user to find out a specific nodes. On that kind of situation we can give a functionally to user so that they can search for the tree node and can be expand automatically if its found.
In this case you can use FindNode()
method to find a particular node and then use Expand()
method to expand that particular node. FindNode() take node name as argument and return TreeNode if it’s found. You may even use ExpandAll()
method to expand all the subsequent child nodes.
Expanding Specific Node using Expand()
Method
protected void buttonSearch_Click(object sender, EventArgs e) { aspTreeView.CollapseAll(); TreeNode searchNode = aspTreeView.FindNode(textSearch.Text); if (searchNode != null) searchNode.Expand(); }
Expanding all subsequent Child Nodes using ExpandAll()
Method
protected void buttonSearch_Click(object sender, EventArgs e) { aspTreeView.CollapseAll(); TreeNode searchNode = aspTreeView.FindNode(textSearch.Text); if (searchNode != null) searchNode.ExpandAll(); }
Pingback: How to expand specific Tree View Node programmatically in ASP.NET … - asp