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 += ...
      }
}    

其他提示

ADD

 OnItemCommand="repNested_ItemCommand"

to

<asp:Repeater ID="rptNested" runat="server" OnItemCommand="repSales_ItemCommand">

CodeBehind:

Protected Sub repNested_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs)


 Select Case e.CommandName
// Do whatever you want
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top