Hi All,
I Have a treeview control in my web application. Whenever user clicks on treeview node, according to node the page is displayed. Now i want to programatically select treeview node by default while page loading. How can i set the selectednode dynamically in asp.net. help me..
Regards,
Nayeem
Loading
Anand UchagawkarPosted Sep 20, 2011, 9:11 AM
If you want to go throughly all the nodes in the TreeView then you will probably will have to recursively loop through all the Nodes & thier subnodes & thier subnodes... to see which node you exactly want to select.
Add the following Class in your code,
public static class TreeViewHelper Descendents(this TreeNode node)
{
public static IEnumerable
{
if (node.ChildNodes.Count > 0)
foreach (TreeNode childNode in node.ChildNodes)
{
yield return childNode;
Descendents(childNode);
}
}
public static IEnumerable Descendents(this TreeView trv)
{
foreach (TreeNode node in trv.Nodes)
{
yield return node;
if (node.ChildNodes.Count > 0)
foreach (TreeNode childNode in node.Descendents())
{
yield return childNode;
}
}
}
}
The class implements an extension method concept, so the class should be either in your same project or reference the ddl in your project in which you will define the class. Once you do this then you can simply modify your code,
foreach (TreeNode nodes in TreeViewControl1.Descendents())
{
if(nodes.Text == "yourCustomText")
{
nodes.Selected = true;
break;
}
}
Here TreeViewControl1 is name of your tree view control on the page. You can see that there is an new method Descendents() getting added in the TreeViewControl which will automatically give a list all nodes & subnodes as well with any depth of the tree.
If this is Helpful, Please make this as ANSWER
Jaganathan BantheswaranPosted Sep 20, 2011, 7:19 AM
Use like this,
foreach (TreeNode tn in node.ChildNodes)
{
if (tn.Text.ToLower() == "yournodename")
{
tn.Selected = true;
break;
}
}