2

我正在尝试将数据表中的节点添加到树视图中。我的问题不是添加节点,而是显示它们。我的循环通过并添加每个节点。我有一个正确显示总节点的文本框。但是,树视图什么也不显示。我错过了一些显示属性吗?

感谢您的帮助!

    oldComments.DataBind()
    Dim count As Integer = 0
    Dim TreeView1 As TreeView = New TreeView
    ' TreeView1.FindNode("My Node").ChildNodes().Add(New TreeNode("Test This"))
    For Each row As DataRow In dsData.Rows
        Dim node As TreeNode = New TreeNode(row("UpdateTimeStamp").ToString)

        Dim node2 As TreeNode = New TreeNode((count.ToString + " - Count"), "test")
        TreeView1.Nodes.Add(node2)
        TreeView1.Nodes.Add(node)
        TreeView1.Nodes(0).ChildNodes().Add(node)
    Next
    TreeView1.ExpandAll()
    status.Text = TreeView1.Nodes.Count

然后是 ASP:

    <asp:TreeView ID="TreeView1" runat="server">
        <Nodes>
            <asp:TreeNode Text="My Node" Value="My Node"></asp:TreeNode>
        </Nodes>
    </asp:TreeView>

我添加了一个节点来查看它在哪里显示/尝试使用查找控件添加一个新的子节点,但没有奏效。建议?

谢谢你。

4

2 回答 2

2

You are assigning the same node twice. Once to the treeview nodes and once to the child nodes of another node. You do not need to assign a node to the treeview itself if you are adding a child to some node. Change the code to

TreeView1.Nodes.Add(node2)
node2.ChildNodes().Add(node)
于 2012-08-14T15:45:56.550 回答
1

我认为问题在于您将所有节点都添加到了错误的TreeView.

您已经TreeView在标记中将 声明为“TreeView1”。所以这一行:

Dim TreeView1 As TreeView = New TreeView

should be removed, and the rest of the code should still work just fine.

This assumes your TreeView is directly on the Page at the "top level" and not in some kind of container (like an UpdatePanel). If that's the case, you would need to first use FindControl to get the TreeView.

So, rather than removing the line above, you would replace it with something like this:

Dim TreeView1 As TreeView = someContainer.FindControl("TreeView1")

Also, that code probably needs to be running in the Page_Load section of your code behind (if it is not already). but I don't really think that's the problem.

于 2012-08-14T15:45:45.740 回答