【发布时间】:2009-03-04 18:45:57
【问题描述】:
我正在使用嵌套数据列表来显示分层数据。在嵌套数据列表中,我希望能够绑定到属于父数据列表绑定到的对象的属性。
有谁知道我如何做到这一点?
【问题讨论】:
-
你的数据结构是什么样子的?
标签: c# asp.net vb.net nested datalist
我正在使用嵌套数据列表来显示分层数据。在嵌套数据列表中,我希望能够绑定到属于父数据列表绑定到的对象的属性。
有谁知道我如何做到这一点?
【问题讨论】:
标签: c# asp.net vb.net nested datalist
我不知道一个干净的存档方法。
Hack 你可能(不想)尝试:
<%#
(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");
}
}
【讨论】: