首先设置TreeView的属性AutoPostBack="True",这样才会有Onchange="TreeView2_ChkChange" 的有效。
Sub TreeView2_ChkChange(ByVal sender As Object, ByVal e As TreeViewClickEventArgs) Handles TreeView2.Check
CheckAllChildNodes(TreeView2.GetNodeFromIndex(e.Node), True)
Session("Groups") = NodeShow(TreeView2.Nodes)
Response.Write("Session=" & Session("Groups"))
End Sub
Sub CheckAllChildNodes(ByVal treeNode As TreeNode, ByVal nodeChecked As Boolean)
Dim node As TreeNode
For Each node In treeNode.Nodes
node.Checked = nodeChecked
If node.Checked = True Then
If node.Nodes.Count > 0 Then
Me.CheckAllChildNodes(node, nodeChecked)
End If
End If
Next
End Sub
Function NodeShow(ByVal treeNode As TreeNodeCollection) As String
Dim node As TreeNode
For Each node In treeNode
If node.CheckBox Then
If node.Checked Then
strGroupIds = strGroupIds & node.Text & ","
End If
If node.Nodes.Count > 0 Then
NodeShow(node.Nodes)
End If
End If
Next
Return strGroupIds
End Function