Nannette Thacker ShiningStar.net

ASP.net Web Application Development

Sponsors

News

See all Blog Posts by Nannette.

Nannette Thacker, consultant and owner of Shining Star Services LLC, specializes in development of custom dynamic database driven web applications utilizing ASP.net technologies. Nannette has been developing ASP sites since 1997. Nannette has written numerous articles on web development techniques and tutorials.

Nannette is the owner and developer of ChristianSinglesDating.com.

 Subscribe in a reader





View Nannette  Thacker's profile on LinkedIn

October 2009 - Posts

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

Posted: Oct 30 2009, 04:23 PM by nannette | with 11 comment(s)
Filed under: ,
More Posts