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

Issue with Expand to Level

4 Answers 164 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
Karthik
Top achievements
Rank 1
Karthik asked on 03 Apr 2012, 07:17 PM
Hi,

I have a treelist control with load on demand enabled. I need to expand items to first level for which i used " ExpandToLevel(1) " . Things were fine when i was having the older version of telerik. I recently upgraded it to the latest version and found this issue. On initial load things are fine. When i try to expand any item all the expanded levels and the buttons for expanding are lost and only the first level items are seen. For more information reg the problem pls check the sample code below.
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                RadTreeList1.ExpandToLevel(1);               
            }           
        }
 
        protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            RadTreeList1.DataSource = GetDataTable("SELECT * FROM TestItems WHERE ParentID IS NULL", null);
        }
 
        protected void RadTreeList1_ChildItemsDataBind(object sender, TreeListChildItemsDataBindEventArgs e)
        {
            int id = Convert.ToInt32(e.ParentDataKeyValues["ID"].ToString());
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@ID";
            param.Value = id;
            e.ChildItemsDataSource = GetDataTable("SELECT * FROM TestItems WHERE ParentID = @ID", param);
        }
 
        private DataTable GetDataTable(string query, SqlParameter param)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand(query, conn);
 
            if (param != null)
            {
                command.Parameters.Add(param);
            }
            adapter.SelectCommand = command;
            DataTable myDataTable = new DataTable();
 
            conn.Open();
            try
            {
                adapter.Fill(myDataTable);
            }
            finally
            {
                conn.Close();
            }
 
            return myDataTable;
        }
 
        private void ConvertEmptyValuesToDBNull(Hashtable values)
        {
            List<object> keysToDbNull = new List<object>();
 
            foreach (DictionaryEntry entry in values)
            {
                if (entry.Value == null || (entry.Value is String && String.IsNullOrEmpty((String)entry.Value)))
                {
                    keysToDbNull.Add(entry.Key);
                }
            }
 
            foreach (object key in keysToDbNull)
            {
                values[key] = DBNull.Value;
            }
        }
 
        private void ExecuteNonQuery(string commandText, Hashtable parameters)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            SqlCommand command = new SqlCommand(commandText, conn);
 
            foreach (DictionaryEntry entry in parameters)
            {
                command.Parameters.AddWithValue(entry.Key.ToString(), entry.Value);
            }
 
            conn.Open();
            try
            {
                command.ExecuteNonQuery();
            }
            finally
            {
                conn.Close();
            }
        }

Thanks,
Karthik

4 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 06 Apr 2012, 01:46 PM
Hi Karthik,

With the current implementation of the load on demand feature and the ExpandAllItem/ExpandToLevel methods, combining them is not supported, as the treelist is not aware of the inner levels of its hierarchy. You could submit this requirement as a feature request and our developers will consider implementing API for recursively exapnding the treelist when load on demand is used.

Regards,
Tsvetina
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
Christopher
Top achievements
Rank 1
answered on 24 May 2012, 01:59 PM
I found a work-around to this issue with the current implementation.  We have a very large hierarchy, which required us to use load on demand.  However, the top of the hierarchy tree is relatively small and the client was insistent that certain levels of the TreeList be initially expanded.  With load on demand enabled I was having a hard time getting this to work for the reasons that Karthik pointed out.  After scratching my head on this for quite some time [ :( ]  I came up with a solution.  Here's the pseudocode:

// method to populate & expand the tree list
public void RadTreeList_Populate()
{
  myRadTreeList.DataSource = myRadTreeListDAO.FindByParentID( null );
  myRadTreeList.DataBind();
 
  ExpandItems();
}
 
// method to initially expand items
protected void ExpandItems()
{
  IList<TreeListDataItem> itemsToExpand = new List<TreeListDataItem>();
 
  do
  {
    itemsToExpand.Clear();
 
    foreach( TreeListDataItem item in myRadTreeList.Items )
    {
      if( ShouldExpandItem( item ) )  // Custom business logic to determine if item should be initially expanded or not
        itemsToExpand.Add( item );
    }
 
    foreach( TreeListDataItem item in itemsToExpand )
    {
      item.FireCommandEvent( "ExpandCollapse", String.Empty );
      myRadTreeList.Rebind();
    }
  } while( itemsToExpand.Count > 0 );
}

Thanks!

-Chris
0
Mark
Top achievements
Rank 1
answered on 12 Nov 2012, 02:27 PM
Where do you call RadTreeList_Populate()?
Do you also need OnNeedDataSource?

Thank you for your answer.

Greetings, Mark
0
Pavlina
Telerik team
answered on 06 Aug 2013, 09:00 AM
Hello,

Since Q2 2013 version of RadControls ExpandAllItem and ExpandToLevel methods are supported with LoadOnDemand. A live example which demonstrates how they work is available here:
http://demos.telerik.com/aspnet-ajax/treelist/examples/databinding/loadondemand/defaultcs.aspx

Regards,
Pavlina
Telerik
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 the blog feed now.
Tags
TreeList
Asked by
Karthik
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Christopher
Top achievements
Rank 1
Mark
Top achievements
Rank 1
Pavlina
Telerik team
Share this question
or