在vb asp.net中,我无法获取所选的TreeView节点

时间:2017-08-06 19:00:58

标签: asp.net vb.net treeview

我遇到的问题是:单击节点时,所选节点索引始终为-1,所选节点文本始终是第一个节点的文本。 正如您在下面的代码中看到的,我已经创建了一个测试应用程序。这个测试应用程序显示了同样的问题

这是我的Default.aspx:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="Treeviewer._Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:TreeView ID="tvDir" OnSelectedNodeChanged="tvDir_SelectedNodeChanged" OnTreeNodeExpanded="tvDir_TreeNodeExpanded" runat="server"></asp:TreeView>
    </div>
    </form>
</body>
</html>

这就是代码隐藏:

Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        tvDir.Nodes.Add(New TreeNode("C:\"))
        For Each aDir As String In IO.Directory.EnumerateDirectories("C:\")
            Dim aNode As TreeNode = New TreeNode(aDir, 0)
            If hasSubDirectories(aDir) Then
                aNode.ChildNodes.Add(New TreeNode("", 1))
            End If
            aNode.SelectAction = TreeNodeSelectAction.Select
            tvDir.Nodes(0).ChildNodes.Add(aNode)
        Next
        tvDir.CollapseAll()

    End Sub

    Protected Sub tvDir_SelectedNodeChanged(sender As Object, e As EventArgs)
        Dim tvIndex = tvDir.Nodes.IndexOf(tvDir.SelectedNode)
        Debug.Write(tvIndex & " " & tvDir.SelectedNode.Text)
    End Sub

    Protected Sub tvDir_TreeNodeExpanded(sender As Object, e As TreeNodeEventArgs)

    End Sub
    Private Function hasSubDirectories(inDirectory As String) As Boolean
        Try
            If IO.Directory.Exists(inDirectory) Then
                Return IO.Directory.EnumerateDirectories(inDirectory).Count > 0
            End If
        Catch ex As Exception
            '
        End Try
        Return False
    End Function

End Class

0 个答案:

没有答案