views:

142

answers:

1

I need to check/select all the nodes in a tree view with minimum complexity. My tree view has 3 levels and many nodes in it. below is my code:

<asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1" ShowCheckBoxes="All" ShowExpandCollapse="true"
            <DataBindings> 
                <asp:TreeNodeBinding DataMember="Category" TextField="Name" 
                    ValueField="Value" />
                <asp:TreeNodeBinding DataMember="LeafCategory" TextField="Name" 
                    ValueField="Value" />
                <asp:TreeNodeBinding DataMember="ChildCategory" TextField="Name" 
                    ValueField="Value" />
                <asp:TreeNodeBinding DataMember="SubCategory" TextField="Name" 
                    ValueField="Value" />
                <asp:TreeNodeBinding DataMember="Categories" TextField="Name" 
                    ValueField="Value" />
            </DataBindings>
        </asp:TreeView>
+1  A: 

Something like this:

Private Sub SetMyChildren(ByVal parentNode As TreeNode, ByVal isChecked As Boolean, ByVal isSelected As Boolean)
    parentNode.Checked = isChecked
    parentNode.Selected = isSelected
    For Each child As TreeNode In parentNode.ChildNodes
        SetMyChildren(child, isChecked, isSelected)
    Next
End Sub


'f.e. set all nodes from complete TreeView to checked and selected:
For Each node As TreeNode In TreeView1.Nodes
    SetMyChildren(node, True, True)
Next
Tim Schmelter