.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Set selected node on treeview asp.net

Posted By:Sunil Yadav       Posted Date: May 21, 2010    Points: 15    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

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;


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend