How to access TreeListDataItem from TreeListDetailTemplateItem

6 posts, 0 answers
  1. Frank
    Frank avatar
    10 posts
    Member since:
    Oct 2011

    Posted 07 Nov 2011 Link to this post

    I'm building a control that runs inside of the DetailsView of Rad TreeList.

    My naming container is a TreeListDetailTemplateItem. From this context, I can't find an easy way to get access to the TreeListDataItem for this row (To get DisplayIndex). If it happens we are data-binding, I can do this hack:

               var detaiItem = (this.NamingContainer as TreeListDetailTemplateItem);
               if (detailItem.DataItem != null)
               {
    //HACK: Cast to my business class, use the key value to find the appropriate treelistDataItem
                   var itemId = (detailItem.DataItem as CheckListItem).CheckListItemID;
                   treeListDataItem  = RadTreeList1.Items.Where(i => (int) i.GetDataKeyValue("CheckListItemID") == itemId).SingleOrDefault();
         var displayIndex = treeListDataItem.DisplayIndex
        .....
               }


    I have the same limitation from within TreeList -> ItemCreated(). When trying to draw a Context menu for my DetailsView body, I run into an issue getting the DisplayIndex. Per this example: http://www.telerik.com/community/forums/aspnet-ajax/treelist/dose-the-tree-list-have-a-context-menu-funcationality.aspx#1428456

    I had to hack around it using this code:

    protected void RadTreeList1_ItemCreated(object sender, TreeListItemCreatedEventArgs e)
    {
        TreeListDataItem treeListDataItem = null;
     
     
        if (e.Item is TreeListDataItem )
        {
            treeListDataItem = (TreeListDataItem)e.Item;   
            treeListDataItem.Attributes["oncontextmenu"] = "ShowContextMenu(" + treeListDataItem.DisplayIndex + ", event);";
             
        }
        else if (e.Item is TreeListDetailTemplateItem)
        {
            // We need the TreeListDataDataItem so we can use it's Display Index
     
     
            var detailItem = (e.Item as TreeListDetailTemplateItem);
            if (detailItem.DataItem != null)
            {
                var itemId = (detailItem.DataItem as CheckListItem).CheckListItemID;
                treeListDataItem  = RadTreeList1.Items.Where(i => (int) i.GetDataKeyValue("CheckListItemID") == itemId).SingleOrDefault();
                detailItem.Attributes["oncontextmenu"] = "ShowContextMenu(" + treeListDataItem.DisplayIndex + ", event);";
            }
        }
             
     
     
    }


    Anyone know some magical cast/path to jump from TreeListDetailTemplateItem to TreeListDataItem? Also, will these controls be implementing IDataItemContainer anytime soon?



  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Nov 2011 Link to this post

    Hello Frank,

    You can try the following code snippet.

    C#:
    protected void RadTreeList1_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
     {
      TreeListDataItem dtitem = null;
      if (e.Item is TreeListDataItem)
       {
         dtitem = e.Item as TreeListDataItem;
         string s = dtitem["FirstName"].Text;
       }
      else if (e.Item is TreeListDetailTemplateItem)
       {
        TreeListDetailTemplateItem detailItem = e.Item as TreeListDetailTemplateItem;
        TreeListDataItem detdataitm = detailItem.Parent.Controls[1] as TreeListDataItem;
        string t = detdataitm["FirstName"].Text;
       }
     }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Frank
    Frank avatar
    10 posts
    Member since:
    Oct 2011

    Posted 08 Nov 2011 Link to this post

    (EDIT)
    Unfortunately my first post thought this was a solution, however it is not. I'm editing this post since it's only been a few minutes.

    Thanks Princy for the suggestion, however your solution will always bring back the first TreeListDataItem. No matter which DetailsTemplate instance I'm in, I will always be reaching back and grabing the first TreeListDataItem. This won't work. I need the TreeListDataItem for the the DetailsTemplate row i'm in.

    I noticed this was the signature for the TreeListDetailtemplateItem class:
    namespace Telerik.Web.UI
    {
        public class TreeListDetailTemplateItem : TreeListItem, INamingContainer
        {
            public TreeListDetailTemplateItem(RadTreeList ownerTreeList, TreeListItemType itemType, bool isDataBinding, TreeListDataItem parentItem);
     
            public virtual object DataItem { get; set; }
            public TableCell TemplateContentCell { get; set; }
     
            public override void Initialize(IList<TreeListColumn> columns);
        }
    }

    It takes a TreeListDataItem in the CTOR. Would be nice if Telerik could expose that to us in some way.
  5. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 11 Nov 2011 Link to this post

    Hi Frank,

    This will be exposed as a property in the upcoming Q3 2011 version of RadControls which is planned for release before the end of next week.

    Currently there is no straight forward way to access the parent of a detail template. You could try using the DataItem property of the detail item to get the record it and its parent are bound to and find the parent by a key value inside this record.

    Kind 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
  6. Leo
    Leo avatar
    1 posts
    Member since:
    Jul 2012

    Posted 16 Jul 2012 Link to this post

    Hi,

    Was this property exposed after all?
    Could you give a quick example of how to use it and get what Frank was asking for?

    Thanks,
    Leo
  7. Tsvetina
    Admin
    Tsvetina avatar
    1876 posts

    Posted 16 Jul 2012 Link to this post

    Hello Leo,

    Yes, there is such property now, the previously used code snippet now would look like this:
    protected void RadTreeList1_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
     {
     if (e.Item is TreeListDetailTemplateItem)
       {
        TreeListDetailTemplateItem detailItem = e.Item as TreeListDetailTemplateItem;
        TreeListDataItem dataitem = detailItem.ParentItem;
        string t = dataitem["FirstName"].Text;
       }
     }


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