Hi,
I want to set the selected node to the treeview on server side, I tried setting selected property to false but it's not working, and always root node is selected. How to set selectednode?
Thanks
I know node value, I don't know node's index so can't iterate through tree nodes like you said above. I've to query .AllNodes and have to find node by value and set that node as selected node to treeview.
Hello kotapati,
You need to iterate through the nodes and to check if HasChildren is true.
If it is you should iterate through its child nodes and select any of them.
For example:
if (WebDataTree1.Nodes[1].HasChildren)
{
WebDataTree1.Nodes[1].Nodes[0].Selected = true;
WebDataTree1.SelectedNodes.Add(WebDataTree1.Nodes[1].Nodes[0]);
}
Let me know if you need further assistance.
If we access node from WebDataTree1.Nodes it would give only root nodes only, suppose if I want to set the selected node as one of the child node of the root nodes. I tried to access child node from .AllNodes collection, but there is no way to find item from AllNodes. How do we find the node anywhere in the tree?
You need to set the selected nodes during a later phase of the WebDataTree lifecycle.
For example inside the PreRender event.
Except setting the node to be selected you need to add it to the SelectedNodes collection.
protected void WebDataTree1_PreRender(object sender, EventArgs e)
DataTreeNode node = (DataTreeNode)WebDataTree1.Nodes.FindNodeByText("Alfreds Futterkiste");
node.Selected = true;
WebDataTree1.SelectedNodes.Add(node);
// another way
WebDataTree1.Nodes[1].Selected = true;
WebDataTree1.SelectedNodes.Add(WebDataTree1.Nodes[1]);
Let me know if you need further assistance regarding this.