ASP.NET How to use nested DataList

<asp:DataList ID="dlParent" runat="server">
<ItemTemplate>
<asp:Label ID="lbl" runat="server"></asp:Label>
<br/>
<asp:DataList ID="dlNested" runat="server">
<ItemTemplate>
<asp:Label ID="lbl" runat="server"></asp:Label>
</ItemTemplate>
</asp:DataList>

</ItemTemplate>
</asp:DataList>



Private Sub dlParent_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs) Handles
dlParent.ItemDataBound

If Not e Is Nothing Then
Dim lbl As Label
Dim dlNested As DataList
lbl = CType(e.Item.FindControl("ParentLabel"), Label)
dlNested = CType(e.Item.FindControl("dlChild"), DataList)

lbl.Text = "content"

AddHandler dlNested .ItemDataBound, AddressOf dlNested_ItemDataBound

dlNested.DataSource = AnyDataSource
dlNested.DataBind()
End If
End Sub

Private Sub dlNested_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataListItemEventArgs)
Dim lbl As Label

If Not e Is Nothing Then
lbl = CType(e.Item.FindControl("lbl"), Label)
lbl = DataBinder.Eval(e.Item.DataItem, "LabelContent").ToString()
End If

End Sub


The key moment is adding new Handler for nested DataList.

Note: AnyDataSource must have field (column) "LabelContent".

No comments:

Post a Comment