views:

171

answers:

1

So I have a TreeView and it has about 7 parent nodes that have 3-5 children each. All of these children nodes when click navigate to a URL. What I would like to do is have one parent node auto-expand based on the URL and the other parent nodes to collapse.

If that wasn't clear, here is an example:

Root
 |
 |--Admin
 |  |
 |  |--Add.aspx
 |  |--Delete.aspx
 |
 |
 |
 |--Purchases
    |
    |--Orders.aspx
    |--Stock.aspx

Lets say the user clicked on Orders.aspx, this would navigate them to that page, and when it does, I would want the tree view to collapse all parent nodes, and expand the current parent node. So Admin would be collapsed and Purchases would be expanded.

What I've attempted so far is this:

Protected Sub resize(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.Load
    For Each node As TreeNode In (CType(sender, TreeView)).Nodes
        If node.NavigateUrl = GetCurrentPage() Then
            For Each parentN As TreeNode In (CType(sender, TreeView)).Nodes
                If Not (parentN.Parent.Selected = True And node.Parent.Text = parentN.Parent.Text) Then
                    parentN.Collapse()
                Else : parentN.Expand()
                End If
            Next
        End If
    Next
End Sub


Public Shared Function GetCurrentPage() As String
    Return System.IO.Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath).ToLower
End Function

I'm not really sure how to go about this.

+1  A: 

Unless your tree is more complicated than you're describing, why won't this loop work?

For Each node As TreeNode In (CType(sender, TreeView)).Nodes
    If node.NavigateUrl = GetCurrentPage() Then
        node.Expand()
    Else
        node.Collapse()
    End If
Next
Dolph
I suppose it would, but what would I put for the Handle? As of now, I have Handles TreeView1.SelectedNodeChanged but that doesn't even seem to execute. I tested by putting a breakpoint in it and it never stops there.
Justen