Search code examples
c#.nettreeviewdevexpressvirtualtreeview

How to maintain checkstate of items in Devexpress Virtual Treeview?


I have a Treeview created in virtual mode which have 4 levels of nodes and at the page load the root element and first level elements are loaded and rest will be loaded based on dynamically using treeFolderList_VirtualModeCreateChildren event.

This is what I have so far

protected void treeFolderList_VirtualModeCreateChildren(object source, TreeViewVirtualModeCreateChildrenEventArgs e)
{
    List<TreeViewVirtualNode> children = new List<TreeViewVirtualNode>();


    if (e.NodeName == null)
    {
        AppendChildNode(children, "root", "All Domains", false);
    }
    else
    {
        if (e.NodeName.Contains("root"))
        {
            PopulateChildNodes(false, children);
        }
        else
        {
            if (!(e.NodeName.StartsWith("u_")))
            {
                PopulateUserChildNodes(false, children, GetDomainBase(e.NodeName), e.NodeName);
            }
            else
            {
                TreeViewVirtualNode tvNode = (TreeViewVirtualNode)treeFolderList.Nodes.FindByName(e.NodeName);
                TreeViewVirtualNode tvNodeParent = (TreeViewVirtualNode)tvNode.Parent;
                string tvParentNodeName = tvNodeParent.Name;
                PopulateUserChildNodes(true, children, GetDomainBase(tvParentNodeName), e.NodeName);
            }
        }
    }
    e.Children = children;
}

This work as expected and it creates the children elements when expanding nodes respectively. My problem is I have check boxes for each node and I need to be able to save the Treeview in a way when I reload/redirect to the page it would reflect the nodes I have selected.

Is there a way to achieve this?


Solution

  • There are several ways to try to achieve this:

    1. Configure ASPxTreeList.SettingsCookies element (probably the easiest solution), in particular the StoreSelection attribute:

      <dvx:ASPxTreeList ... >
      ...
          <SettingsCookies Enabled="true" StoreSelection="true"  />
      ...
      </dvx:ASPxTreeList>
      
    2. If SettingsCookies doesn't work try saving and restoring the TreeList layout manually using ASPxTreeList.ClientLayout event. Define the event handler first:

      <dvx:ASPxTreeList OnClientLayout="dvxTreeList_ClientLayout"... >
      ...
      </dvx:ASPxTreeList>
      

      and follow the example in the doc to handle this event. This way ASPxTreeView as well as the ASPxGridView provide the node/column format data in the e.LayoutData string which can be saved to DB and then restored back.

    3. Store the TreeView selected node keys in Session and restore them using callbacks:

      Example: ASPxTreeList - How to store the selection between requests

    I personally would not rely on cookies and would try methods #2 and #3 first. #2 has been working nicely for me with ASPxGridView and #3 we use in a complex ASPxTreeView setup which also tracks the selection of hidden nodes.

    I hope the examples in the linked docs should be easy enough for you to copy and modify. If not, comment what is not working for you.

    HTH