views:

22014

answers:

5

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?

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.

UPDATE: I have come up with a solution below that sub-classes the TreeView control.

A: 

Easiest way - if it doesn't interfere with the rest of your code - is to simply set the node as not selected in the SelectedNodeChanged method.

protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e){
  // Do whatever you're doing
  TreeView1.SelectedNode.Selected = false;
}
Wayne
Unfortunately I still want to see the Selected Node as being actually selected
BlackMael
A: 

You can always use the MouseDown or MouseUp event and check to see if it the selected node.

I am concerned with the ASP.NET TreeView control at this point. I assume you are referring to the WinForm or WPF controls perhaps? Or are you referring DOM events? I don't really want to delve into scripting some client code for this.
BlackMael
+1  A: 
Larry Smithmier
This does not help unfortunately because anything could cause the postback. I still cannot tell if the SelectedNode was clicked
BlackMael
A: 

When you're adding nodes to the tree in the _TreeNodePopulate() event, set the .SelectionAction property on the node.

TreeNode newCNode;
newCNode = new TreeNode("New Node");

newCNode.SelectAction = TreeNodeSelectAction.Select;

//now you can set the .NavigateUrl property to call the same page with some query string parameter to catch in the page_load()

newCNode.NavigateUrl = "~/ThisPage.aspx?args=" + someNodeAction

RootNode.ChildNodes.Add(newCNode);
Drell
SelectAction is already set. The problem is still I do not know if selecting an already selected node caused the postback.
BlackMael
+2  A: 

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
BlackMael
Shame the code visualiser doesn't seem to like VB.NET :(
BlackMael