Select A Node ASP.NET TreeView Control Programatically

Posted on 2009-12-21
Last Modified: 2012-05-08
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>"



            ElseIf (node.ChildNodes) IsNot Nothing Then


            End If



    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>"



            ElseIf childNode.ChildNodes IsNot Nothing Then


            End If


    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


    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

Question by:npl77
    LVL 35

    Expert Comment

    by:Miguel Oz
    Your code seems OK, you can force the call to TreeView1_SelectedNodeChanged by adding it on line 22.
                   ' 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 works.

    Author Comment

    How do you add the call can you show me? Not too good with event handling
    LVL 35

    Accepted Solution

    Replace line 37 to 39 with:
    Private Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
    End Sub

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

    Replace line 22 with:

    Thus you call the same shared implementation that will fire when the user selects a node or your code selects it programatically

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now