LoadOnDemand and Expand All

3 posts, 1 answers
  1. Esteban
    Esteban avatar
    2 posts
    Member since:
    Dec 2007

    Posted 16 Sep 2008 Link to this post

    Hello all,

    I've looked through the site a bit and found some info, but nothing that was exactly my problem, so here goes.

    I have two treeviews TreeViewStart and TreeViewEnd. TreeViewStart is used kind of like a source for children. I have built this to be Load On Demand, and that part works wonderfully. I have also built it so that I can drag and drop a node and all its children to TreeViewEnd. That also works fine.

    My problem, is that after the user has selected how the treeview should be (TreeViewEnd) using nodes from TreeViewStart, I needed to get the xml of that tree. So when the user clicks a button at the bottom I run the .GetXml function and get back my string. The problem with this is that I only get back information on the already expanded nodes. I believe this problem to be due to the load on demand and the non-existance of child nodes. The .ExpandAll() doesnt seem to work for me probably for the same reason.

    Is there a way to make it so that when the user clicks the "Complete" button, that it will Expand all nodes on TreeViewEnd so that the .GetXml return string is accurate without forcing the user to click through all the child nodes? Below are some snippets of code.

    Thanks in advance.

        <table border="1" cellspacing="0" cellpadding="0" width="100%"><tr> 
            <!-- FROM --> 
            <td align="left" valign="middle" width="49%">  
                <rad:RadTreeView ID="TreeViewStart" EnableDragAndDrop="True" OnNodeDrop="RadTreeView1_HandleDrop" LoadingStatusPosition="BelowNodeText" OnNodeExpand="TreeViewStart_NodeExpand" OnNodeClick="TreeViewStart_NodeClick" runat="server">  
                    <Nodes> 
                        <rad:RadTreeNode Text="Home" ExpandMode="ServerSideCallBack" Value="-1" /> 
                    </Nodes> 
                </rad:RadTreeView> 
            </td> 
              
            <!-- SPACER --> 
            <td width="2%" /> 
              
            <!-- TO --> 
            <td align="left" valign="middle" width="49%">  
                <rad:RadTreeView ID="TreeViewEnd" EnableDragAndDrop="True" LoadingStatusPosition="BelowNodeText" OnNodeExpand="TreeViewEnd_NodeExpand" OnNodeClick="TreeViewStart_NodeClick" runat="server">  
                    <Nodes> 
                        <rad:RadTreeNode Text="Home" ExpandMode="ServerSideCallBack" Value="-1" /> 
                    </Nodes> 
                </rad:RadTreeView> 
            </td> 
        </tr></table>  
          
        <br /> 
        <br /> 
        <br /> 
        <asp:Button ID="btnGetXML" Text="Get XML String" OnClick="btnGetXML_Click" runat="server" /> 

        #region "Private Methods"  
        private void LoadChildNodes(usp_Selectappdata_Role_Recursive NodeRequest, ref RadTreeNodeEventArgs e)  
        {  
            Selectappdata_Role_RecursiveDS.usp_Selectappdata_Role_RecursiveDataTable ChildNodes = Vault<LookUpUtil>().RoleTreeViewSelect(NodeRequest);  
     
            foreach (Selectappdata_Role_RecursiveDS.usp_Selectappdata_Role_RecursiveRow row in ChildNodes)  
            {  
                RadTreeNode node = new RadTreeNode();  
                node.Text = row.RoleName;  
                node.Value = row.AppdataRoleId.ToString();  
                if (row.ChildCount > 0)  
                {  
                    node.ExpandMode = TreeNodeExpandMode.ServerSide;  
                }  
                e.Node.Nodes.Add(node);  
            }  
            e.Node.Expanded = true;  
        }
        #endregion  
     
        protected void Page_Load(object sender, EventArgs e)  
        {  
     
        }  
     
        protected void TreeViewStart_NodeExpand(object sender, RadTreeNodeEventArgs e)  
        {  
            usp_Selectappdata_Role_Recursive NodeRequest = new usp_Selectappdata_Role_Recursive();  
     
            if (e.Node.Value != "-1")  
            {  
                    NodeRequest.Rolelevel = 1;  
                    NodeRequest.RoleLevelCriteria = "=";  
                    NodeRequest.AppdataRoleId = Convert.ToInt32(e.Node.Value);  
            }  
            else 
            {  
                {  
                    NodeRequest.Rolelevel = 0;  
                    NodeRequest.RoleLevelCriteria = "=";  
                }  
            }  
     
            LoadChildNodes(NodeRequest, ref e);  
        }  
     
        protected void TreeViewEnd_NodeExpand(object sender, RadTreeNodeEventArgs e)  
        {  
            usp_Selectappdata_Role_Recursive NodeRequest = new usp_Selectappdata_Role_Recursive();  
     
            if (e.Node.Value != "-1")  
            {  
                NodeRequest.Rolelevel = 1;  
                NodeRequest.RoleLevelCriteria = "=";  
                NodeRequest.AppdataRoleId = Convert.ToInt32(e.Node.Value);  
     
                LoadChildNodes(NodeRequest, ref e);  
            }  
        }  
     
        protected void TreeViewStart_NodeClick(object sender, RadTreeNodeEventArgs e)  
        {  
     
        }  
     
        protected void RadTreeView1_HandleDrop(object sender, RadTreeNodeDragDropEventArgs e)  
        {  
            RadTreeNode sourceNode = e.SourceDragNode;  
            RadTreeNode destNode = e.DestDragNode;  
            RadTreeViewDropPosition dropPosition = e.DropPosition;  
     
            string result = "";  
     
            if (destNode != null)//drag&drop is performed between trees  
            {  
                if (sourceNode.TreeView.SelectedNodes.Count <= 1)  
                {  
     
                    if (!sourceNode.IsAncestorOf(destNode))  
                    {  
                        //result += "<b>" + sourceNode.Text + "</b>" + ";";  
                        sourceNode.Owner.Nodes.Remove(sourceNode);  
                        destNode.Nodes.Add(sourceNode);  
                    }  
                }  
                else if (sourceNode.TreeView.SelectedNodes.Count > 1)  
                {  
                    foreach (RadTreeNode node in TreeViewStart.SelectedNodes)  
                    {  
                        if (!node.IsAncestorOf(destNode))  
                        {  
                            //result += "<b>" + node.Text + "</b>" + ";";  
                            node.Owner.Nodes.Remove(node);  
                            destNode.Nodes.Add(node);  
                        }  
                    }  
                }  
     
                destNode.Expanded = true;  
                //DragMessage.Text = "You dragged node(s)" + result + " onto node <b>" + destNode.Text + "</b>";  
                sourceNode.TreeView.ClearSelectedNodes();  
            }  
        }  
     
        protected void btnGetXML_Click(object sender, EventArgs e)  
        {  
            string preFix = TreeViewEnd.GetXml();  
            //preFix.Replace("<", "&lt;");  
            //preFix.Replace(">", "&gt;");  
            lblXMLString.Text = "Code: " + preFix;  
     
            System.IO.TextWriter asd = new System.IO.StreamWriter(@"C:\exper\test.txt");  
            asd.Write(preFix);  
            asd.Close();  
        } 
  2. Answer
    Simon
    Admin
    Simon avatar
    2281 posts

    Posted 18 Sep 2008 Link to this post

    Hi Esteban,

    You are right, the GetXml method returns the current state of the TreeView and since the not expanded Nodes' children are not available (not loaded at all) they are not part of that state.

    Another issue here is that the ExpandAllNodes method works only for Nodes with ExpandMode.ClientSide (more info on the issue here) and it does not help.

    So, one should load the Nodes before calling the GetXml method.

    Please consider the following approach:
    • Upon clicking on the 'Complete' button: select data (the whole hierarchy) for all parent Nodes of RadTreeViewEnd.
    • Create a temporary RadTreeView and bind it to this hierarchical data.
    • Get its Xml with the GetXml method - it should contain the whole hierarchy of the Nodes dragged and dropped in RadTreeViewEnd.
    I hope this helps.

    Sincerely yours,
    Simon
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Esteban
    Esteban avatar
    2 posts
    Member since:
    Dec 2007

    Posted 19 Sep 2008 Link to this post

    I guess that would work. I might have to go through the tree node structure and call the DB on nodes that are not expanded tho because the point of this app is to allow the user to redefine the hierarchy. However, since the db table is made up of a parent/child relationship that should not be hard to do. Then I could do as you said and load the temp treeview and retrieve the xml formatted string.

    Thanks a bunch!
Back to Top