Nested Repeater AddHandler ItemCommand Not Firing

For those of you who program mostly in code behind, like I do, I have a gotcha for a nested repeater addhandler.

The nested repeater is defined in the code in front:
<asp:Repeater ID="repTestKeyControl" runat="server">

Your nested repeater contains a button that needs to fire a click event, so you add a "CommandName."

<asp:Button ID="btnEdit" runat="server" Text="Edit" CommandName="cmdEdit" />

In the codebehind, you typically retrieve your nested repeater and add the handlers:

                Dim repTestKeyControl As Repeater
                repTestKeyControl = CType(e.Item.FindControl("repTestKeyControl"), Repeater)
                AddHandler repTestKeyControl.ItemCommand, AddressOf repTestKeyControl_ItemCommand
                AddHandler repTestKeyControl.ItemDataBound, AddressOf repTestKeyControl_ItemDataBound  ' programmatically add the handler...
                repTestKeyControl.DataBind()    ' handlers must go before databind


You setup your ItemCommand:

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

        If e.CommandName = "cmdEdit" Then

        End If

    End Sub

But it doesn't fire. Your ItemDataBound fires, so why not the ItemCommand?

I mean, typically a non-nested repeater uses this event and it triggers via the Handles modifier:

    Protected Sub repTestKeyControl_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repTestKeyControl.ItemCommand

So why not a nested repeater? Well, I don't know why, but I know how to fix it. Add the handler in the code-in-front via the "OnItemCommand":

<asp:Repeater ID="repTestKeyControl" runat="server" OnItemCommand="repTestKeyControl_ItemCommand">

Now it works. Go figure. Hope that helps at least someone.

May your dreams be in ASP.NET!

Nannette Thacker

8 Comments

Comments have been disabled for this content.