How to bind DataList inside a DataList?

1.1k Views Asked by At

! am having a nested DataList and I want to bind inner DataList on button event, but it's not working, anyone have an idea?

DataList2.FindControl("DataList3").DataBind();
2

There are 2 best solutions below

0
On

Try to cast it with datalist

((DataList)DataList2.FindControl("DataList3")).DataBind();
0
On

Use this code to Bind Inner DataList :

protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item) || 
             (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        // get inner datalist
        DataList dl3 = e.Item.FindControl("DataList3") as DataList;

        // bind inner datalist with data source
        dl3.DataSource = dt; // DataTable that contains data
        dl3.DataBind();
    }
}

Note: Make sure you have entered this event OnItemDataBound="DataList2_ItemDataBound" like this:

<asp:DataList ID="DataList2" runat="server" OnItemDataBound="DataList2_ItemDataBound">