Issue retrieving value from templated nodes being moved across trees

2 posts, 0 answers
  1. Theresa Repta
    Theresa Repta avatar
    8 posts
    Member since:
    Jul 2009

    Posted 06 Nov 2009 Link to this post

    I have two trees each being bound to a data table with a custom template.

    When I drag nodes from one tree to the other, I need to be able to get the "value" of the node begin dragged as well as its child values.  The problem I'm having is that when trying to access the value of the node and its children in the "_HandlDrop" method, the correct values are not always being associated with the node or its children.

    This only seems to be a problem when I'm using Templates:

    In the example below.  When I move "New Sites Value 3" to the tree on the right, It says the values of the nodes being copied are "2,7,4,5" instead of "3,7,8,9"

    <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
            <div> 
                <asp:Label ID="Label1" runat="server" Height="100px" Text="" Width="338px"></asp:Label> 
             
            <br /><br /> 
                <table> 
                    <tr> 
                        <td> 
                            <table> 
                                <tr> 
                                    <td> 
                                        <telerik:RadTreeView EnableDragAndDrop="true" EnableDragAndDropBetweenNodes="true" ID="RadTreeView1" OnNodeDrop="RadTreeSelectedGroup_HandleDrop" runat="server" OnNodeDataBound="RadTreeView1_NodeDataBound" />         
                                    </td> 
                                </tr> 
                            </table> 
                             
                        </td> 
                        <td> 
                            <table> 
                                <tr> 
                                    <td> 
                                        <telerik:RadTreeView  EnableDragAndDrop="true" EnableDragAndDropBetweenNodes="true" ID="RadTreeView2" runat="server" OnNodeDataBound="RadTreeView1_NodeDataBound" />         
                                    </td> 
                                </tr> 
                            </table> 
                             
                        </td> 
                    </tr> 
                </table> 
                 
            </div> 

    Code:

     public partial class TempTest : System.Web.UI.Page 
        { 
            protected void Page_Load(object sender, EventArgs e) 
            { 
                if (!Page.IsPostBack) 
                { 
                    DataTable table = new DataTable(); 
                    table.Columns.Add("ID"); 
                    table.Columns.Add("ParentID"); 
                    table.Columns.Add("Text"); 
                    table.Columns.Add("Url"); 
                    table.Columns.Add("Value"); 
     
                    table.Rows.Add(new String[] { "1"null"All Sites""""1" }); 
                    table.Rows.Add(new String[] { "2""1""Search Engines""""2" }); 
                    table.Rows.Add(new String[] { "3""1""News Sites""""3" }); 
                    table.Rows.Add(new String[] { "4""2""Yahoo""http://www.yahoo.com""4" }); 
                    table.Rows.Add(new String[] { "5""2""MSN""http://www.msn.com""5" }); 
                    table.Rows.Add(new String[] { "6""2""Google""http://www.google.com""6" }); 
                    table.Rows.Add(new String[] { "7""3""CNN""http://www.cnn.com""7" }); 
                    table.Rows.Add(new String[] { "8""3""BBC""http://www.bbc.co.uk""8" }); 
                    table.Rows.Add(new String[] { "9""3""FOX""http://www.foxnews.com""9" }); 
     
     
     
                    DataTable table2 = new DataTable(); 
                    table2.Columns.Add("ID"); 
                    table2.Columns.Add("ParentID"); 
                    table2.Columns.Add("Text"); 
                    table2.Columns.Add("Url"); 
                    table2.Columns.Add("Value"); 
     
                    table2.Rows.Add(new String[] { "1"null"All Sites""""1" }); 
                    table2.Rows.Add(new String[] { "2""1""Search Engines""""2" }); 
                    table2.Rows.Add(new String[] { "3""1""News Sites""""3" }); 
                    table2.Rows.Add(new String[] { "4""2""Yahoo""http://www.yahoo.com""4" }); 
                    table2.Rows.Add(new String[] { "5""2""MSN""http://www.msn.com""5" }); 
                    table2.Rows.Add(new String[] { "6""2""Google""http://www.google.com""6" }); 
                    table2.Rows.Add(new String[] { "7""3""CNN""http://www.cnn.com""7" }); 
                    table2.Rows.Add(new String[] { "8""3""BBC""http://www.bbc.co.uk""8" }); 
                    table2.Rows.Add(new String[] { "9""3""FOX""http://www.foxnews.com""9" }); 
     
     
     
                    RadTreeView1.DataTextField = "Text"
                    RadTreeView1.DataNavigateUrlField = "Url"
                    RadTreeView1.DataFieldID = "ID"
                    RadTreeView1.DataValueField = "Value"
                    RadTreeView1.DataFieldParentID = "ParentID"
                    RadTreeView1.DataSource = table; 
                    RadTreeView1.DataBind(); 
     
                    RadTreeView2.DataTextField = "Text"
                    RadTreeView2.DataNavigateUrlField = "Url"
                    RadTreeView2.DataFieldID = "ID"
                    RadTreeView2.DataValueField = "Value"
                    RadTreeView2.DataFieldParentID = "ParentID"
                    RadTreeView2.DataSource = table2; 
                    RadTreeView2.DataBind(); 
     
                } 
            } 
     
            protected void RadTreeSelectedGroup_HandleDrop(object sender, Telerik.Web.UI.RadTreeNodeDragDropEventArgs e) 
            { 
                RadTreeNode sourceNode = e.SourceDragNode; 
                RadTreeNode destNode = e.DestDragNode; 
                RadTreeViewDropPosition dropPosition = e.DropPosition; 
     
                List<RadTreeNode> childNodes = new List<RadTreeNode>(); 
     
                childNodes.Add(sourceNode); 
     
                childNodes = FindChildNodes(sourceNode, childNodes); 
     
     
     
                int[] TaskTemplateIds = new int[childNodes.Count]; 
                for (int i = 0; i < childNodes.Count; i++) 
                { 
                    TaskTemplateIds[i] = int.Parse(childNodes[i].Value); 
                } 
     
                for (int i = 0; i < TaskTemplateIds.Length; i++) 
                { 
                    Label1.Text += TaskTemplateIds[i].ToString(); 
                } 
     
     
     
     
            } 
     
            List<RadTreeNode> FindChildNodes(RadTreeNode currentNode, List<RadTreeNode> nodeLst) 
            { 
                 
                foreach (RadTreeNode n in currentNode.Nodes) 
                { 
                     
                    nodeLst.Add(n); 
                    FindChildNodes(n, nodeLst); 
                } 
                return nodeLst; 
            } 
     
     
            class RootNodeTemplate : ITemplate 
            { 
                public void InstantiateIn(Control container) 
                { 
                    Label label1 = new Label(); 
                    label1.ID = "ItemLabel"
                    label1.Text = "Text"
                    label1.Font.Size = 10; 
                    label1.Font.Bold = true
                    label1.DataBinding += new EventHandler(label1_DataBinding); 
                    container.Controls.Add(label1); 
                } 
                private void label1_DataBinding(object sender, EventArgs e) 
                { 
                    Label target = (Label)sender; 
                    RadTreeNode node = (RadTreeNode)target.BindingContainer; 
                    string nodeText = (string)DataBinder.Eval(node, "Text"); 
                    target.Text = nodeText + "Value is " + node.Value; 
                } 
            } 
     
            class ChildNodeTemplate : ITemplate 
            { 
                public void InstantiateIn(Control container) 
                { 
                    Label label1 = new Label(); 
                    label1.ID = "ItemLabel2"
                    label1.Text = "Text"
                    label1.Font.Size = 8; 
                    label1.ForeColor = System.Drawing.Color.Blue; 
                    label1.DataBinding += new EventHandler(label1_DataBinding); 
                    container.Controls.Add(label1); 
                } 
                private void label1_DataBinding(object sender, EventArgs e) 
                { 
                    Label target = (Label)sender; 
                    RadTreeNode node = (RadTreeNode)target.BindingContainer; 
                    string nodeText = (string)DataBinder.Eval(node, "Text"); 
                    target.Text = nodeText + " Value is " + node.Value; 
                } 
            } 
            protected void RadTreeView1_NodeDataBound(object sender, RadTreeNodeEventArgs e) 
            { 
                RootNodeTemplate template = new RootNodeTemplate(); 
                ChildNodeTemplate template1 = new ChildNodeTemplate(); 
     
                if (e.Node.Level == 0) 
                    template.InstantiateIn(e.Node); 
                else 
                    template1.InstantiateIn(e.Node); 
     
                e.Node.DataBind(); 
            } 
        } 


  2. Yana
    Admin
    Yana avatar
    5015 posts

    Posted 12 Nov 2009 Link to this post

    Hello Theresa,

    I'm sorry for the delay.

    The node templates should be set in Page_Load event, please try it like this:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("ID");
                table.Columns.Add("ParentID");
                table.Columns.Add("Text");
                table.Columns.Add("Url");
                table.Columns.Add("Value");
     
                table.Rows.Add(new String[] { "1", null, "All Sites", "", "1" });
                table.Rows.Add(new String[] { "2", "1", "Search Engines", "", "2" });
                table.Rows.Add(new String[] { "3", "1", "News Sites", "", "3" });
                table.Rows.Add(new String[] { "4", "2", "Yahoo", "http://www.yahoo.com", "4" });
                table.Rows.Add(new String[] { "5", "2", "MSN", "http://www.msn.com", "5" });
                table.Rows.Add(new String[] { "6", "2", "Google", "http://www.google.com", "6" });
                table.Rows.Add(new String[] { "7", "3", "CNN", "http://www.cnn.com", "7" });
                table.Rows.Add(new String[] { "8", "3", "BBC", "http://www.bbc.co.uk", "8" });
                table.Rows.Add(new String[] { "9", "3", "FOX", "http://www.foxnews.com", "9" });
     
     
     
                DataTable table2 = new DataTable();
                table2.Columns.Add("ID");
                table2.Columns.Add("ParentID");
                table2.Columns.Add("Text");
                table2.Columns.Add("Url");
                table2.Columns.Add("Value");
     
                table2.Rows.Add(new String[] { "1", null, "All Sites", "", "1" });
                table2.Rows.Add(new String[] { "2", "1", "Search Engines", "", "2" });
                table2.Rows.Add(new String[] { "3", "1", "News Sites", "", "3" });
                table2.Rows.Add(new String[] { "4", "2", "Yahoo", "http://www.yahoo.com", "4" });
                table2.Rows.Add(new String[] { "5", "2", "MSN", "http://www.msn.com", "5" });
                table2.Rows.Add(new String[] { "6", "2", "Google", "http://www.google.com", "6" });
                table2.Rows.Add(new String[] { "7", "3", "CNN", "http://www.cnn.com", "7" });
                table2.Rows.Add(new String[] { "8", "3", "BBC", "http://www.bbc.co.uk", "8" });
                table2.Rows.Add(new String[] { "9", "3", "FOX", "http://www.foxnews.com", "9" });
     
     
     
                RadTreeView1.DataTextField = "Text";
                RadTreeView1.DataNavigateUrlField = "Url";
                RadTreeView1.DataFieldID = "ID";
                RadTreeView1.DataValueField = "Value";
                RadTreeView1.DataFieldParentID = "ParentID";
                RadTreeView1.DataSource = table;
                RadTreeView1.DataBind();
     
                RadTreeView2.DataTextField = "Text";
                RadTreeView2.DataNavigateUrlField = "Url";
                RadTreeView2.DataFieldID = "ID";
                RadTreeView2.DataValueField = "Value";
                RadTreeView2.DataFieldParentID = "ParentID";
                RadTreeView2.DataSource = table2;
                RadTreeView2.DataBind();
     
            }
     
            RootNodeTemplate template = new RootNodeTemplate();
            ChildNodeTemplate template1 = new ChildNodeTemplate();
     
            foreach (RadTreeNode node in RadTreeView1.GetAllNodes())
            {
                if (node.Level == 0)
                    template.InstantiateIn(node);
                else
                    template1.InstantiateIn(node);
     
                node.DataBind();
            }
             
        }

    and let us know how it goes.

    All the best,
    Yana
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top