I have a repeater nested in the ItemTemplate
of a parent Repeater. I usually connect them to controls declaratively in markup.
<asp:Repeater runat='server' id='myParentRepeater'>
<ItemTemplate>
<asp:Repeater runat='server' id='mynestedRepeater' OnItemCommand='myMethod'>
...
</asp:Repeater>
...
</ItemTemplate>
</asp>
Today I decided to do it in the code-behind, specifically in the ItemDataBound
method of the parent repeater./
((Repeater)e.item.FindControl("MyParentRepeater")).ItemCommand += ...
But it won't work this way. Unless I use markup the event handler won't fire. Why is this? Or, assuming it should work, can someone tell me what have I done wrong?
The ItemDataBound
event is triggered only when the Repeater
is databound and not on every postback. But events must be recreated on every postback. Hence use the ItemCreated
event instead.
protected void myParentRepeater_ItemCreated(Object Sender, RepeaterItemEventArgs e) {
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
((Repeater)e.item.FindControl("MyParentRepeater")).ItemCommand += ...
}
}