如何捕获单击TreeView的Selected节点的事件?它不会触发SelectedNodeChanged,因为选择显然没有改变但是我可以捕获什么事件所以我知道单击了所选节点?
更新:当我有一些时间,我将不得不深入了解TreeView控件的内容并挖掘它处理click事件的内容和位置,并将TreeView子类化以暴露新事件OnSelectedNodeClicked.
我可能会在圣诞节假期这样做,我会报告结果.
更新:我已经在TreeView控件的子类下面提出了一个解决方案.
最简单的方法 - 如果它不干扰你的其余代码 - 就是简单地将节点设置为SelectedNodeChanged方法中未选择的节点.
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e){ // Do whatever you're doing TreeView1.SelectedNode.Selected = false; }
经过一段漫长的时间后,我终于有时间研究如何将TreeView子类化以处理被点击的选定节点.
这是我的解决方案,它公开了一个新的事件SelectedNodeClicked,您可以从Page或任何地方处理它.(如果需要,重构为C#是一项简单的任务)
Imports System.Web.UI Imports System.Web Public Class MyTreeView Inherits System.Web.UI.WebControls.TreeView Public Event SelectedNodeClicked As EventHandler Private Shared ReadOnly SelectedNodeClickEvent As Object Private Const CurrentValuePathState As String = "CurrentValuePath" Protected Property CurrentValuePath() As String Get Return Me.ViewState(CurrentValuePathState) End Get Set(ByVal value As String) Me.ViewState(CurrentValuePathState) = value End Set End Property Friend Sub RaiseSelectedNodeClicked() Me.OnSelectedNodeClicked(EventArgs.Empty) End Sub Protected Overridable Sub OnSelectedNodeClicked(ByVal e As EventArgs) RaiseEvent SelectedNodeClicked(Me, e) End Sub Protected Overrides Sub OnSelectedNodeChanged(ByVal e As System.EventArgs) MyBase.OnSelectedNodeChanged(e) ' Whenever the Selected Node changed, remember its ValuePath for future reference Me.CurrentValuePath = Me.SelectedNode.ValuePath End Sub Protected Overrides Sub RaisePostBackEvent(ByVal eventArgument As String) ' Check if the node that caused the event is the same as the previously selected node If Me.SelectedNode IsNot Nothing AndAlso Me.SelectedNode.ValuePath.Equals(Me.CurrentValuePath) Then Me.RaiseSelectedNodeClicked() End If MyBase.RaisePostBackEvent(eventArgument) End Sub End Class