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

How to access TreeListDataItem from TreeListDetailTemplateItem

5 Answers 195 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 1
Frank asked on 07 Nov 2011, 07:10 PM
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?



5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Nov 2011, 09:03 AM
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.
0
Frank
Top achievements
Rank 1
answered on 08 Nov 2011, 05:34 PM
(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.
0
Tsvetina
Telerik team
answered on 11 Nov 2011, 12:52 PM
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
0
Leo
Top achievements
Rank 1
answered on 16 Jul 2012, 01:21 PM
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
0
Tsvetina
Telerik team
answered on 16 Jul 2012, 03:27 PM
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.
Tags
TreeList
Asked by
Frank
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Frank
Top achievements
Rank 1
Tsvetina
Telerik team
Leo
Top achievements
Rank 1
Share this question
or