[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1527
  • Last Modified:

Select A Node ASP.NET TreeView Control Programatically

I am creating treeview nodes dynamically in code when I create a node I set the
newNode.SelectAction = TreeNodeSelectAction.Select
which makes the node selectable  and when I manually click on a node the TreeView1.SeletedIndexChanged fires as expected.

The problem comes in in the Search functionality. The code to search for a name locates the proper name in the treeview and changes its font color to lime which is the functionality I want.

What I need the search to do it not only change the font color (which it does already) but also select the node and fire the SelectedNodeChanged event programatically. I added "node.Select() to my code but it does nothing.

Can anyone help. Thanks in advance.
Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
      
        For Each node As TreeNode In TreeView1.Nodes
            If (node.Text = txtSearch.Text) Then
                node.Text = "<span style='color: lime'>" & node.Text & "</span>"
                ExpandAllNodes(node)
                node.Select()
            ElseIf (node.ChildNodes) IsNot Nothing Then
                CheckChildNodes(node)

            End If
        Next

        TreeView1.ExpandAll()
    End Sub

    Private Sub CheckChildNodes(ByVal node As TreeNode)
        For Each childNode As TreeNode In node.ChildNodes
            If childNode.Text = txtSearch.Text Then
                childNode.Text = "<span style='color: lime'>" & childNode.Text & "</span>"
                ExpandAllNodes(childNode)
                childNode.Select()
            ElseIf childNode.ChildNodes IsNot Nothing Then
                CheckChildNodes(childNode)
            End If
        Next
    End Sub

    Private Sub ExpandAllNodes(ByVal node As TreeNode)
        Dim nodeParent As TreeNode = node.Parent
        While (nodeParent.Parent IsNot Nothing)
            nodeParent = nodeParent.Parent
        End While
        nodeParent.ExpandAll()
    End Sub

    Private Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
        Label1.Text = "Hi " + TreeView1.SelectedNode.Text
    End Sub

Open in new window

0
npl77
Asked:
npl77
  • 2
  • 2
1 Solution
 
Miguel OzSoftware EngineerCommented:
Your code seems OK, you can force the call to TreeView1_SelectedNodeChanged by adding it on line 22.
               childNode.Select()
               ' add the call here I will put as a separate method (DoSelectedNodeChanged(ByVal selected As TreeNode)

If you select the node in code, the   TreeView1_SelectedNodeChanged will not fired. It is just the way asp.net works.
0
 
npl77Author Commented:
How do you add the call can you show me? Not too good with event handling
0
 
Miguel OzSoftware EngineerCommented:
Replace line 37 to 39 with:
Private Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
        DoSelectedNodeChanged(TreeView1.SelectedNode)
End Sub

Private Sub DoSelectedNodeChanged(ByVal selected As TreeNode)
        ' Put your common code here to use selected  
End Sub

Replace line 22 with:
  childNode.Select()
  DoSelectedNodeChanged(childNode)

Thus you call the same shared implementation that will fire when the user selects a node or your code selects it programatically
0
 
npl77Author Commented:
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now