how do i catch events of a datalist in a repeater? I need to use datalist's ItemDataBound event but i cant use it because it is inside a repeater control.
Thanks in advance

Dani AI

Generated

Good call by — nested controls inside a Repeater often need their events hooked up manually. Two reliable patterns work and are safe years later: attach the child DataList handler dynamically while the Repeater item is being bound, or declare the DataList event in markup and add the handler method by hand in code-behind.

Example (dynamic attach inside Repeater.ItemDataBound):

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataList dl = e.Item.FindControl("DataList1") as DataList;
        if (dl != null)
        {
            dl.ItemDataBound += new DataListItemEventHandler(ChildDataList_ItemDataBound);
            // set child datasource here if each child has its own data
            // dl.DataSource = ...
            // dl.DataBind();
        }
    }
}

protected void ChildDataList_ItemDataBound(object sender, DataListItemEventArgs e)
{
    DataList child = (DataList)sender;
    RepeaterItem parent = child.NamingContainer as RepeaterItem;
    // use parent.ItemIndex or stored keys to correlate parent/child
}

Markup alternative (declare event in the nested DataList):

<asp:DataList ID="DataList1" runat="server" OnItemDataBound="ChildDataList_ItemDataBound">

Then implement the same ChildDataList_ItemDataBound signature manually in code-behind (Visual Studio may not auto-generate it for nested controls).

Troubleshooting notes: always check e.Item.ItemType before touching items; if you need the parent row's DataItem inside the child handler, capture the needed key/value before you call the child DataBind() (parent.DataItem is often null later). To avoid duplicate subscriptions, attach handlers when the child control is created/bound (not repeatedly on postback), or prefer the DataList ItemCommand for button clicks to reduce per-item handlers.

I found it, you manually create the event and event handler( no visual studio support since the datalist control is in the repeater control). I thought it was like in .net 1.1, which would give an error because you didnt registered the event, but it worked successfully when i did that manually.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.