How to expand specific Tree View Node programmatically in ASP.NET ?

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.

image

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();

}

image

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();

}

image

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.

One Comment to “How to expand specific Tree View Node programmatically in ASP.NET ?”

Comments are closed.