【问题标题】:Nested Datalists in ASP.netASP.net 中的嵌套数据列表
【发布时间】:2009-03-04 18:45:57
【问题描述】:

我正在使用嵌套数据列表来显示分层数据。在嵌套数据列表中,我希望能够绑定到属于父数据列表绑定到的对象的属性。

有谁知道我如何做到这一点?

【问题讨论】:

  • 你的数据结构是什么样子的?

标签: c# asp.net vb.net nested datalist


【解决方案1】:

我不知道一个干净的存档方法。

Hack 你可能(不想)尝试:

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

<%# 
     Eval(
        DataBinder.GetDataItem(Container.BindingContainer...BindingContainer)
        ,"PropertyOfParentsDataListDataItem"
     )
 %>

【讨论】:

    【解决方案2】:

    我不知道如何内联,但如果你连接到 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");
    
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多