3

我正在学习如何访问 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 没有,但就是这样。再次感谢大家的帮助。

4

2 回答 2

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

希望这会有所帮助

于 2013-02-12T16:22:54.880 回答
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

我还建议至少暂时使用 aDirectCast而不是,以确保找到控件。CType你可以这样实现:

Dim objTreeView as TreeView = DirectCast(objLoginView.FindControl("TreeViewMain"), TreeView)
If objTreeView IsNot Nothing Then
    'The control was found
End If
于 2013-02-12T16:25:35.407 回答