Skip to main content

How to: Prohibit Collapsing Nodes

The sample code below represents an ASPxTreeView.ExpandedChanging event handler. The event handler prevents collapsing nodes if it has selected children in any generation.

View Example

<dx:ASPxTreeView ID="ASPxTreeView1" runat="server" DataSourceID="XmlDataSource1" AutoPostBack="True" AllowSelectNode="True" OnExpandedChanging="ASPxTreeView1_ExpandedChanging">
</dx:ASPxTreeView>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/MenuTabbedMenu.xml"
    XPath="/mainmenu/item"></asp:XmlDataSource>
using DevExpress.Web.ASPxTreeView;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e) {

    }
    protected void ASPxTreeView1_ExpandedChanging(object source, DevExpress.Web.ASPxTreeView.TreeViewNodeCancelEventArgs e) {
        if ((e.Node.Expanded) && (ASPxTreeView1.SelectedNode != null)) {
            TreeViewNode  node = ASPxTreeView1.SelectedNode.Parent;
            while (node != null) {
                if (e.Node == node) {
                    e.Cancel = true;
                    break;
                }
                node = node.Parent;
            }
        }
    }
}