views:

958

answers:

2

I am using nested datalists to display hierarchical data. In the nested datalist i want to be able to bind to a property that belongs to the object that the parent datalist is bound to.

does anyone know how I can achieve this ?

+1  A: 

I dont know a clean way to archive this.

Hack you may (not) want to try:

<%# 
     (DataBinder.GetDataItem(Container.BindingContainer...BindingContainer) as AType)
     .PropertyOfParentsDataListDataItem 
 %>

or

<%# 
     Eval(
        DataBinder.GetDataItem(Container.BindingContainer...BindingContainer)
        ,"PropertyOfParentsDataListDataItem"
     )
 %>
Bartek Szabat
+1  A: 

I don't know how to do it inline, but if you hook into OnItemDataBound you can use the following code:

Protected Sub YourList_ItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs) Handles YourList.ItemDataBound

  If e.Item.ItemType = ListItemType.Item Or _
    e.Item.ItemType = ListItemType.AlternatingItem Then

    CType(e.Item.FindControl("LabelName"), Label).Text = _
       DataBinder.Eval(CType(sender.Parent, DataListItem).DataItem, "FieldName"))

  End If

End Sub

or in C# (unverified)

Protected Void YourList_ItemDataBound(Object sender, DataListItemEventArgs e)
{
   if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem )
  {
    ((Label)e.Item.FindControl("LabelName")).Text = 
       DataBinder.Eval(((DataListItem)sender.Parent).DataItem, "FieldName");

  }
}
Tom Halladay