OnSelectedNodeChanged event in asp.net treeview not working

Here is my code in .aspx ,

  <asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged" ImageSet="Arrows" > </asp:TreeView> <asp:TextBox runat="server" ID="selectedNode"></asp:TextBox> 

In my .cs file

  protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e) { selectedNode.Text = TreeView1.SelectedNode.Value; } 

But the OnSelectedNodeChanged event OnSelectedNodeChanged not working, I want to know why.

I am linking nodes dynamically. I used the link.

+4
source share
3 answers

You are right, this will not work, because the link says

  root.SelectAction = TreeNodeSelectAction.Expand; 

Instead, write // use .Select

  root.SelectAction = TreeNodeSelectAction.Select; 

He will work.

 TreeNodeSelectAction.Expand Toggles the node between expanded and collapsed. Raises the TreeNodeExpanded event or the TreeNodeCollapsed event, as appropriate. TreeNodeSelectAction.None Raises no events when a node is selected. TreeNodeSelectAction.Select Raises the SelectedNodeChanged event when a node is selected. TreeNodeSelectAction.SelectExpand Raises both the SelectedNodeChanged and TreeNodeExpanded events when a node is selected. Nodes are only expanded, never collapsed. 
+2
source

This is an old thread, but another reason OnSelectedNodeChanged does not work is if you have set the value of NavigateURL for this node. There is a trick you could use. Remove the NavigateURL property and set the node property "Value" to the URL to which you want to link, and then into the fire of the OnSelectedNodeChanged event in the code behind, redirect the URL from the SelectedNode.Value property.

NTN

Dave

0
source

I had similar problems. When I remove NavigateURL for nodes, when they are dynamically generated, the OnSelectedNodeChanged event occurs.

0
source

All Articles