Issue with Expand to Level

5 posts, 0 answers
  1. Karthik
    Karthik avatar
    18 posts
    Member since:
    Aug 2010

    Posted 03 Apr 2012 Link to this post

    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
  2. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 06 Apr 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Christopher
    Christopher avatar
    1 posts
    Member since:
    Jan 2012

    Posted 24 May 2012 Link to this post

    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
  5. Mark
    Mark avatar
    1 posts
    Member since:
    Nov 2012

    Posted 12 Nov 2012 Link to this post

    Where do you call RadTreeList_Populate()?
    Do you also need OnNeedDataSource?

    Thank you for your answer.

    Greetings, Mark
  6. Pavlina
    Admin
    Pavlina avatar
    6084 posts

    Posted 06 Aug 2013 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017