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 c
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.
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