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 ?
我不知道存档的干净方法。
哈克你可能(不想)想尝试:
<%#
(DataBinder.GetDataItem(Container.BindingContainer...BindingContainer) as AType)
.PropertyOfParentsDataListDataItem
%>
或者
<%#
Eval(
DataBinder.GetDataItem(Container.BindingContainer...BindingContainer)
,"PropertyOfParentsDataListDataItem"
)
%>
我不知道如何内联,但如果你连接到 OnItemDataBound ,你可以使用以下代码:
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
或在 C# 中(未经验证)
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");
}
}