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;
}

Recent Comments