ASP.Net TreeView node extension from code

I am learning how to access ASP.Net master page controls and try to extend a specific TreeView node. I am doing this from a different page that is not the main page.

objContentPlaceHolder, objLoginView and objTreeView all have a value confirmed with the debugger.

Can you take a look at this code and tell us why the code in the for loop is not executing? It reaches the for loop, but just skips the for loop at that point.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim objContentPlaceHolder As ContentPlaceHolder
    Dim objLoginView As LoginView
    Dim objTreeView As TreeView

    objContentPlaceHolder = CType(Master.FindControl("ContentPlaceHolderBody"), ContentPlaceHolder)

    If Not objContentPlaceHolder Is Nothing Then

        objLoginView = CType(objContentPlaceHolder.FindControl("loginViewMain"), LoginView)

        If Not objLoginView Is Nothing Then
            objTreeView = CType(objLoginView.FindControl("TreeViewMain"), TreeView)

            ' Make sure all nodes for Maintenance are expanded.
            '--------------------------------------------------
            For Each treenode As TreeNode In objTreeView.Nodes
                If treenode.Text = "Maintenance" Then
                    treenode.Expand()
                End If
            Next treenode
        End If
    End If
End Sub

      

* Update *

I changed the page load event handler to the PreRenderComplete event handler and would it work? Not sure why PreRender didn't do it. Thanks again to everyone for their help.

+3


source to share


2 answers


   public Sub TreeView_TreeNodeDataBound(ByVal sender As Object, ByVal e As TreeNodeEventArgs  )
       dim mapNode as SiteMapNode =  e.Node.DataItem as SiteMapNode
       If mapNode.Title = "Maintenance" then
           e.Node.Expand()
       End if
   End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim objContentPlaceHolder As ContentPlaceHolder
        Dim objLoginView As LoginView
        Dim objTreeView As TreeView

        objContentPlaceHolder = CType(Master.FindControl("ContentPlaceHolderBody"), ContentPlaceHolder)

        If Not objContentPlaceHolder Is Nothing Then

            objLoginView = CType(objContentPlaceHolder.FindControl("loginViewMain"), LoginView)

            If Not objLoginView Is Nothing Then
                objTreeView = CType(objLoginView.FindControl("TreeViewMain"), TreeView)
                objTreeView.TreeNodeDataBound += TreeView_TreeNodeDataBound 
            End If
        End If
    End Sub

      



Hope this helps

+1


source


In your example, it looks like your logic is only checking the root nodes. When working with hierarchical data, you need to use recursive logic to ensure that the entire structure is evaluated.

Something like this you need:

Protected Sub btnSearch_Click(sender As Object, e As EventArgs)
    For Each node As TreeNode In TreeView1.Nodes
        ExpandNodeByValue("Maintenance", node)
    Next
End Sub

Private Sub ExpandNodeByValue(value As String, parentNode As TreeNode)
    For Each childNode As TreeNode In parentNode.ChildNodes
        If childNode.Value.ToLower() = value.ToLower() Then
            childNode.Expand()
        End If
        If childNode.ChildNodes.Count > 0 Then
            ExpandNodeByValue(value, childNode)
        End If
    Next
End Sub

      



I would also suggest using DirectCast

instead CType

, at least temporarily, to make sure the control is found. You would accomplish it like this:

Dim objTreeView as TreeView = DirectCast(objLoginView.FindControl("TreeViewMain"), TreeView)
If objTreeView IsNot Nothing Then
    'The control was found
End If

      

+1


source







All Articles