Back to Devexpress

Selection

aspnet-3969-components-tree-list-concepts-focus-and-navigation-selection.md

latest6.4 KB
Original Source

Selection

  • Aug 23, 2023
  • 3 minutes to read

The ASPxTreeList allows users to select (or clear) a node’s check box to select (or deselect) the corresponding node.

Run Demo

Set the Enabled property to true to display the node check boxes. Set the AllowSelectAll property to true to display a check box in the column header panel that selects/deselects all nodes.

aspx
<dx:ASPxTreeList ID="treeList" runat="server" DataSourceID="DepartmentsDataSource" KeyFieldName="ID">
    <SettingsSelection Enabled="True" AllowSelectAll="True"/>
    <!-- ... -->

Set a node’s AllowSelect property to false to hide the node’s check box and prevent the node from being selected.

The example below shows how to prevent users from selecting a node that has no child nodes.

csharp
TreeListNodeIterator iterator = treeList.CreateNodeIterator();
TreeListNode node;
while (true) {
    node = iterator.GetNext();
    if (node == null) break;
    node.AllowSelect = node.HasChildren; 
}
vb
Dim iterator As TreeListNodeIterator = treeList.CreateNodeIterator()
Dim node As TreeListNode
While True
    node = iterator.GetNext()
    If node Is Nothing Then Exit While
    node.AllowSelect = node.HasChildren
End While

Refer to the following section for the complete example: How to: Select Nodes That Meet the Specified Criteria.

Recursive Selection

Set the Recursive property to true to make recursive selection available.

aspx
<dx:ASPxTreeList ID="treeList" runat="server" DataSourceID="DepartmentsDataSource" KeyFieldName="ID">
    <SettingsSelection Enabled="True" Recursive="True"/>
    <!-- ... -->

When recursive selection is available for users:

  • A parent node is automatically selected when all its child nodes are selected. Deselecting a child node automatically deselects its parent node(s).

  • When a user selects a parent node, its child nodes are automatically selected - including child nodes that are hidden with a filter.

  • A node that has selected and unselected child nodes is displayed with a grayed check box.

Refer to the following section for the complete example: How to: Enable Recursive Selection When Binding at Runtime.

Example: How to Store the Selection Between Requests

aspx
<dx:ASPxTreeList ID="treeList" runat="server" ClientInstanceName="treeList" AutoGenerateColumns="False"
    KeyFieldName="ItemId" ParentFieldName="ParentId" KeyboardSupport="True" OnDataBinding="atlSelection_DataBinding" OnCustomCallback="treeList_CustomCallback">
    <Columns>
        <dx:TreeListTextColumn FieldName="Code" />
        <dx:TreeListTextColumn FieldName="Name" />
        <dx:TreeListTextColumn FieldName="Description" />
        <dx:TreeListTextColumn FieldName="ItemType" />
        <dx:TreeListTextColumn FieldName="Price" >
            <PropertiesTextEdit DisplayFormatString="{0:C}" />
        </dx:TreeListTextColumn>
    </Columns>
    <SettingsPager PageSize="20" Mode="ShowPager" />
    <SettingsBehavior AllowFocusedNode="true" AutoExpandAllNodes="True" 
                      AllowSort="False" FocusNodeOnExpandButtonClick="true" />
    <SettingsSelection Enabled="True" />
    <SettingsEditing AllowNodeDragDrop="true" Mode="EditForm" />
</dx:ASPxTreeList>

<dx:ASPxButton ID="ASPxButton1" runat="server" Text="Save selection" AutoPostBack="false">
    <ClientSideEvents Click="function (s, e) { treeList.PerformCallback('Save'); }" />
</dx:ASPxButton>
<dx:ASPxButton ID="ASPxButton2" runat="server" Text="Restore selection" AutoPostBack="false">
    <ClientSideEvents Click="function (s, e) { treeList.PerformCallback('Restore'); }" />
</dx:ASPxButton>
csharp
private const string selectedNodesKey = "selectedNodesKey";

protected void treeList_CustomCallback(object sender, TreeListCustomCallbackEventArgs e) {
    switch (e.Argument) {
        case "Save":
            SaveSelection();
            break;
        case "Restore":
            RestoreSelection();
            break;
    }
}
private void SaveSelection() {
    Session[selectedNodesKey] = treeList.GetSelectedNodes().Select(node => node.Key);
}
private void RestoreSelection() {
    if (Session[selectedNodesKey] == null)
        return;
    var rowKeys = (IEnumerable<string>)Session[selectedNodesKey];
    var iterator = treeList.CreateNodeIterator();
    iterator.Reset();

    while (iterator.Current != null) {
        iterator.Current.Selected = rowKeys.Contains(iterator.Current.Key);
        iterator.GetNext();
    }
}
vb
Private Const selectedNodesKey As String = "selectedNodesKey"

Protected Sub treeList_CustomCallback(ByVal sender As Object, ByVal e As TreeListCustomCallbackEventArgs)
    Select Case e.Argument
        Case "Save"
            SaveSelection()
        Case "Restore"
            RestoreSelection()
    End Select
End Sub

Private Sub SaveSelection()
    Session(selectedNodesKey) = treeList.GetSelectedNodes().Select(Function(node) node.Key)

End Sub
Private Sub RestoreSelection()
    If Session(selectedNodesKey) Is Nothing Then
        Return
    End If
    Dim rowKeys = DirectCast(Session(selectedNodesKey), IEnumerable(Of String))
    Dim [iterator] = treeList.CreateNodeIterator()
    [iterator].Reset()

    Do While [iterator].Current IsNot Nothing
        [iterator].Current.Selected = rowKeys.Contains([iterator].Current.Key)
        [iterator].GetNext()
    Loop
End Sub

See Also

Member Table: Selection