c# – asp.net treeview中的OnSelectedNodeChanged事件不起作用

这是我在.aspx中的代码,

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

在我的.cs文件中

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

但是,OnSelectedNodeChanged事件不起作用,我想知道原因.

我动态绑定节点.我用了这个link.

最佳答案 你是对的,它将无法正常工作,因为它在链接中被写入

 root.SelectAction = TreeNodeSelectAction.Expand;

而不是写//使用.Select

  root.SelectAction = TreeNodeSelectAction.Select;

它会工作.

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.
点赞