Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > TreeView > Convert RadTreeNode to RadTreeNodeData or vice versa in a RadTreeView?

Not answered Convert RadTreeNode to RadTreeNodeData or vice versa in a RadTreeView?

Feed from this thread
  • Fahmi avatar

    Posted on Jun 22, 2011 (permalink)

    Is there any built-in method to convert a RadTreeNode object to a RadTreeNodeData object or vice versa in a RadTreeView?

    Reply

  • Nikolay Tsenkov Nikolay Tsenkov admin's avatar

    Posted on Jun 23, 2011 (permalink)

    Hello Fahmi,

    You can bind a TreeView instance (server-side) with a list of TreeNodeData type (see Binding to Array, ArrayList and Generic List: http://www.telerik.com/help/aspnet-ajax/treeview-data-binding-array.html) and then get all the nodes created in the TreeView - treeView.GetAllNodes().


    Regards,
    Nikolay Tsenkov
    the Telerik team

    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.

    Reply

  • Joel Kraft avatar

    Posted on Jan 4, 2012 (permalink)

    This isn't a really great solution since you can only possibly get Text, NavigateUrl and Value bound to the nodes, and RadTreeNodeData contains a lot more information.

    There really should be a constructor, or a cast, or a ToNode() method to create a node from the Data.  This would allow the data returned from a webservice or page method to be easily reused on the server side.

    Reply

  • Plamen Zdravkov Plamen Zdravkov admin's avatar

    Posted on Jan 6, 2012 (permalink)

    Hello Joel,

    Thank you for the suggestion. We will consider implementing it in the future.

    Would you please explain the exact scenario where you need this functionality so we can inspect it and compare it with other possible workarounds.

    Kind regards,
    Plamen Zdravkov
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now

    Reply

  • Joel Kraft avatar

    Posted on Jan 31, 2012 (permalink)

    The scenario we ran into was this:

    • Root-level tree nodes represent periods of time.
    • Only the node for the current period should be expanded at load.
    • Other nodes' children are lazy-loaded upon expanding (using page method call).
    • We wanted to use the same method to load child nodes whether it was happening during page load or as part of a node expanding.

    Here's the workaround we ended up using:

    private void Page_Load()
    {
        // snip SQL code to load periods
        using (SqlDataReader r = sqlh.ExecuteReader())
        {
            while (r.Read())
            {
                // create node for the period
                RadTreeNode n = new RadTreeNode();
                // if this is the current period
                if (r.GetString(5)[0] == 'Y')
                {
                    // load children
                    foreach (RadTreeNodeData data in GetTermNodes(residentId, termId))
                    {
                        n.Nodes.Add(NodeFromData(data));
                    }
                    n.Expanded = true;
                    n.ExpandMode = 0;
                }
            }
        }
      
    [WebMethod]
    public static RadTreeNodeData[] LoadNodes(RadTreeNodeData node, object context)
    {  
        string[] chunks = node.Value.Split('-');
        if (chunks.Length < 2) return CreateNodeData("Could not get resident and term ID.");
        int residentId;
        if (!chunks[0].TryParseInt32(out residentId)) return CreateNodeData("Invalid resident ID.");
        short termId;
        if (!chunks[1].TryParseInt16(out termId)) return CreateNodeData("Invalid term ID.");
        return GetTermNodes(residentId, termId).ToArray(); 
    }
     
    private static List<RadTreeNodeData> GetTermNodes(int residentId, short termId)
    {
        List<RadTreeNodeData> nodes = new List<RadTreeNodeData>();
        // snip code to generate child nodes
        return nodes;
    }
     
     
    private static RadTreeNode NodeFromData(RadTreeNodeData data)
    {
        RadTreeNode node = new RadTreeNode(data.Text, data.Value, data.NavigateUrl);
        node.ContextMenuID = data.ContextMenuID;
        node.ExpandMode = data.ExpandMode;
        node.ImageUrl = data.ImageUrl;
        return node;
    }
    <TELERIK:RadTreeView ID="tvTerms" runat="server">
        <WebServiceSettings Path="Default.aspx" Method="LoadNodes" />
    </TELERIK:RadTreeView>

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET AJAX > TreeView > Convert RadTreeNode to RadTreeNodeData or vice versa in a RadTreeView?
Related resources for "Convert RadTreeNode to RadTreeNodeData or vice versa in a RadTreeView?"

ASP.NET TreeView Features  |   Documentation   |  Demos  |  Telerik TV  |  Self-Paced Trainer  |  Step-by-step Tutorial  ]