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
Hello kotapati,
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.
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 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.
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 kopati,
This is what you should do.
I think that is because of no view state available in raise call back event . what i was trying to do was finding the node and set as selected node in the raise call back event where the was no view state and all the node text is empty.
I tried using the above code...for finding the child nodes with in the tree,, but WebDataTree1.AllNodes is giving correct node count but treeNode.Text having empty "" string for all the nodes. Is it the right way of finding for a node by text?
I haven't shared any code snippet before because I thought that you were asking about confirmation.
Yes, you should iterate through All nodes of the WebDataTree and check the text / value of any of them.
If it matches you should simply set selected to true and add the item to the SelectedNodes collection.
foreach (var item in WebDataTree1.AllNodes)
DataTreeNode treeNode = (DataTreeNode)item;
if (treeNode.Text.Equals("Chocolate")) {
treeNode.Selected = true;
WebDataTree1.SelectedNodes.Add(treeNode);
Let us know if you need further assistance regarding this
There is no code in your post. Can you please check that.