RadTreeList ChildItems list is null

568 views Asked by At

I have a problem with Telerik's RadTreeList control. When I load data into the RadTreeList, I can access its children only when I click the Expand button. Without expanding, ChildItems list is empty. Is there a way to get children of a particular parent without expanding?

1

There are 1 answers

0
Reg Edit On

The child items are only present in the control when the parent item is expanded. However, it's possible to expand an item in code in order to access the child items. (You can then restore the parent to its collapsed state if you don't want it to be displayed expanded.)

A bit of care's needed because of when things happen in the control's lifecycle:

private bool isGetChildItems = false;

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack)
    {
        //if ( your condition that requires access to  child items )
        {
            isGetChildItems = true;
            //your logic to expand whichever nodes you need
            RadTreeList1.Items[0].Expanded = true;
        }
    }
}

protected void RadTreeList1_DataBound(object sender, EventArgs e)
{
    //At this point in the lifecycle we can access the child items
    if (isGetChildItems)
    {
        //Do whatever it is we need to do with the child items
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", string.Format("alert('Item 0 has {0} child items')", RadTreeList1.Items[0].ChildItems.Count), true);
    }
}

protected void RadTreeList1_PreRender(object sender, EventArgs e)
{
    if (isGetChildItems)
    {
        //Restore node state, clear our flag and rebind
        isGetChildItems = false;
        RadTreeList1.Items[0].Expanded = false;
        RadTreeList1.DataBind();
    }
}