Skip to main content

An Infinite Loop Occurs in the SelectionChanged Event Handler

  • 2 minutes to read

When you call the DxTreeView.ClearSelection or DxAccordion.ClearSelection method in the corresponding component’s SelectionChanged event handler, an infinite loop may happen if URL matching is enabled.

<DxAccordion @ref="MyAccordion"
             SelectionMode="NavigationSelectionMode.Single"
             UrlMatchMode="NavigationUrlMatchMode.Prefix"
             SelectionChanged="@SelectionChanged">
    <Items>
        <DxAccordionItem Text="Metals" />
        <DxAccordionItem Text="Metalloids" NavigateUrl="/" />
        <DxAccordionItem Text="Nonmetals" />
    </Items>
</DxAccordion>

@code {
    DxAccordion MyAccordion;
    void SelectionChanged(AccordionSelectionChangedEventArgs e) {
        MyAccordion.ClearSelection();
    }
}

If at least one of the specified NavigateURL property values corresponds to an empty address (/) or matches the current browser URL, the component endlessly iterates through the following steps:

  • The component selects an item/node according to the specified UrlMatchMode.
  • The SelectionChanged event fires with UrlMatch as an item state change reason.
  • The event handler calls the ClearSelection method.
  • The SelectionChanged event fires again with ApiCall as the item state change reason. This change forces the component to select an item/node according to the specified UrlMatchMode again (the first step).

To resolve this issue, you can set the UrlMatchMode property to None if you do not need URLs to be matched. Alternatively, you can modify the SelectionChanged event handler so that the ClearSelection method is not called after the component matches URLs:

@code {
    DxAccordion MyAccordion;
    void SelectionChanged(AccordionSelectionChangedEventArgs e) {
        if (e.Reason != NavigationItemStateChangeReason.UrlMatch) {
            MyAccordion.ClearSelection();
        }
    }
}