从代码隐藏扩展ASP.Net TreeView节点

时间:2013-02-12 15:56:23

标签: asp.net vb.net treeview code-behind expand

我正在学习如何访问ASP.Net母版页的控件并尝试扩展特定的TreeView节点。我是从另一个不是母版页的页面做的。

objContentPlaceHolder,objLoginView和objTreeView都有一个使用调试器确认的值。

你能看一下这段代码,让我们知道为什么for循环中的代码没有执行?它到达for循环但只是跳过for循环。

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

*更新*

我将页面加载事件处理程序更改为PreRenderComplete事件处理程序,您认为它有效吗?不知道为什么PreRender没有,但就是这样。再次感谢所有人的帮助。

2 个答案:

答案 0 :(得分:1)

   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

希望这会有所帮助

答案 1 :(得分:1)

从您的示例中,您的逻辑看起来只是检查根节点。处理分层数据时,您需要使用递归逻辑来确保整个结构得到评估。

这就是你需要的东西:

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

我还建议至少暂时使用DirectCast代替CType,以确保找到控件。你可以像这样实现:

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