ASP.NET TreeView和选择所选择的节点 [英] ASP.NET TreeView and Selecting the Selected Node
问题描述
我如何捕捉一个TreeView的点击所选节点的事件?
它不火的 SelectedNodeChanged 因为选择显然没有改变,但我能赶上那什么大事,所以我知道,选择的节点被点击了?
How do I capture the event of the clicking the Selected Node of a TreeView? It doesn't fire the SelectedNodeChanged since the selection has obviously not changed but then what event can I catch so I know that the Selected Node was clicked?
更新:
当我有一段时间,我将不得不深入到TreeView控件的深处,挖掘出什么,在哪里它处理click事件和子类的TreeView揭露一个新的事件OnSelectedNodeClicked。
UPDATE: When I have some time, I'm going to have to dive into the bowels of the TreeView control and dig out what and where it handles the click events and subclass the TreeView to expose a new event OnSelectedNodeClicked.
我可能会做到这一点在圣诞节假期,我会的结果汇报。
I'll probably do this over the Christmas holidays and I'll report back with the results.
更新:
我想出了低于子类的解决方案TreeView控件。
UPDATE: I have come up with a solution below that sub-classes the TreeView control.
推荐答案
稍长一段之后,我终于有了一些时间来考虑如何继承TreeView的处理所选节点被点击。
After a somewhat lengthy period, I have finally had some time to look into how to subclass the TreeView to handle a Selected Node being clicked.
下面是我的解决方案,它暴露了一个新的事件的 SelectedNodeClicked 您可以从页面或其它地方处理。
(如果需要的话它是一个简单的任务,重构为C#的)
Here is my solution which exposes a new event SelectedNodeClicked which you can handle from the Page or wherever. (If needed it is a simple task to refactor into 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
这篇关于ASP.NET TreeView和选择所选择的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!