Home > ASP.Net, C# > set selected node on treeview asp.net

set selected node on treeview asp.net

This article will help you to set the selected node of ASP.NET Tree View based on node text. I did this for my application and it works fine.
Following is the code to select node.

TreeNode node { get; set; }

private TreeNode SelectNode(string nodetext, TreeNodeCollection parentCollection)
{
foreach (TreeNode childnode in parentCollection)
{
//iterate through the treeview nnode
if (childnode.Text == nodetext)
{
node = childnode;
}
else if (childnode.ChildNodes.Count > 0)
{
// check for child item(level 2)
node = GetNode(nodetext, childnode.ChildNodes);
}
//if Match found return node
if ((node != null)) break;
}
return node;
}

Here is the way to call the function and set the selected node.

TreeNode node1 = SelectNode(“nodetext”, tvMenu.Nodes);
if (node1 != null)
{
node1.Selected = true;
}

Advertisement
Categories: ASP.Net, C# Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.