[![在此处输入图像说明] [1]] [1]我的树视图的设计视图:
<asp:TreeView ID="TreeView1" ShowExpandCollapse="true" runat="server" ShowCheckBoxes="Leaf"
ViewStateMode="Enabled">
</asp:TreeView>
代码:
Private Sub MyProject_Load(sender As Object, e As EventArgs) Handles Me.Load
' MY DATATABLE WITH THE VALUES:
Dim dt as datatable = mymethodtoreadvaluesfrombd()
TreeView1.Nodes.Clear()
Dim node As TreeNode
Dim subNode As TreeNode
For Each row As DataRow In dt.Rows
node = Searchnode(row.Item(2).ToString(), TreeView1)
If node IsNot Nothing Then
subNode = New TreeNode(row.Item(3).ToString())
subNode.ShowCheckBox = True
node.ChildNodes.Add(subNode)
Else
node = New TreeNode(row.Item(2).ToString())
subNode = New TreeNode(row.Item(3).ToString())
node.ChildNodes.Add(subNode)
TreeView1.Nodes.Add(node)
End If
Next
End Sub
Private Function Searchnode(ByVal nodetext As String, ByVal trv As TreeView) As TreeNode
For Each node As TreeNode In trv.Nodes
If node.Text = nodetext Then
Return node
End If
Next
End Function
我有一个按钮,当我点击它时,我需要阅读我的树视图中检查的子节点。 我找不到尝试多种代码的方式,有人能建议我吗?感谢。
答案 0 :(得分:1)
您可以执行第二个for...each
循环来遍历子节点:
For Each Parentnode as TreeNode in TreeVeiw1.Nodes
For Each Childnode as Treenode in Parentnode.Nodes
If Childnode.checked Then
End If
Next
Next
我对ID的建议(请参阅下面的评论)是使用Childnode.Tag
,大多数控件都包含.Tag
,您可以在其中存储数据。
答案 1 :(得分:0)
我在代码中看到了问题,
问题是,当我点击按钮时会出现一个autopostback事件,它会删除树视图复选框,所以我把它放在加载中,直接转到按钮动作点击:
Private Sub MyProject_Load(sender As Object, e As EventArgs) Handles Me.Load
If not is postback then
' create the treeview
end if
End Sub