Rendered Tree Items

2 posts, 1 answers
  1. Ivan
    Ivan avatar
    9 posts
    Member since:
    Dec 2010

    Posted 14 Feb 2011 Link to this post


    I'd like to know how to get from the radtreelist, the dataitems that will be rendered after requesting a node expansion, and in which event it could be done.

    I've tried the following code, but without success.

    protected void TreeList_Load(object sender, EventArgs e)
         MyTreeList.DataSource = ws.GetSales(); 
         //This retrieves an array of objects which represent the sales of several stores,
         //their managers and their employees
         TreeListDataItemCollection rendered_items = MyTreeList.Items;
         object[] data = rendered_items.Select(s => s.DataItem).ToArray(); 
         //I thought this would get the DataBoundItems
         //in the treelist, but instead it gets an Array with null objects
         //Code for Exporting the desired data

    By the way, getting the Items from "MyTreeList" in the OnLoad event, retrieves me the Items shown in the previous state of the control, not the ones that were recently asked for, so I don't know which event could work better. Could you help me?

  2. Answer
    Tsvetina avatar
    2101 posts

    Posted 18 Feb 2011 Link to this post

    Hello Ivan,

    You can retrieve the new items in RadTreeList after it is expanded or collapsed in its DataBound event. In order to perform this action only after expand or collapse has happened, you can wire the ItemCommand event and raise a flag if the command name is ExpandCollapse. Here is a portion of sample code demonstrating this approach:
    bool isExpandedOrCollapsed = false;
    protected void RadTreeList1_ItemCommand(object sender, TreeListCommandEventArgs e)
        if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            isExpandedOrCollapsed = true;
    protected void RadTreeList1_DataBound(object sender, EventArgs e)
        if (isExpandedOrCollapsed)
            List<TreeListDataItem> items = new List<TreeListDataItem>();
            foreach (TreeListDataItem item in RadTreeList1.Items)
        isExpandedOrCollapsed = false;

    As for the Load event of the control, same as with the other RadControls, it is fired before the postback changes have happened. You can see the lifecycle of RadControls in this help article.

    Best wishes,
    the Telerik team
Back to Top