This is a migrated thread and some comments may be shown as answers.

Convert RadTreeNode to RadTreeNodeData or vice versa in a RadTreeView?

4 Answers 185 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Fahmi
Top achievements
Rank 1
Fahmi asked on 23 Jun 2011, 02:47 AM
Is there any built-in method to convert a RadTreeNode object to a RadTreeNodeData object or vice versa in a RadTreeView?

4 Answers, 1 is accepted

Sort by
0
Nikolay Tsenkov
Telerik team
answered on 23 Jun 2011, 03:31 PM
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.

0
Joel Kraft
Top achievements
Rank 2
answered on 04 Jan 2012, 06:33 PM
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.
0
Plamen
Telerik team
answered on 06 Jan 2012, 04:44 PM
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
0
Joel Kraft
Top achievements
Rank 2
answered on 31 Jan 2012, 03:07 PM
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>
Tags
TreeView
Asked by
Fahmi
Top achievements
Rank 1
Answers by
Nikolay Tsenkov
Telerik team
Joel Kraft
Top achievements
Rank 2
Plamen
Telerik team
Share this question
or