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

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 12 comment(s)
Filed under: ,

Comments

Nested Repeater AddHandler ItemCommand Not Firing | I love .NET! said:

Pingback from  Nested Repeater AddHandler ItemCommand Not Firing | I love .NET!

# October 30, 2009 5:30 PM

Nested Repeater AddHandler ItemCommand Not Firing - Nannette Thacker ShiningStar.net said:

Pingback from  Nested Repeater AddHandler ItemCommand Not Firing - Nannette Thacker ShiningStar.net

# October 30, 2009 6:06 PM

Servefault.com said:

Thank you for submitting this cool story - Trackback from Servefault.com

# October 31, 2009 2:41 AM

Alexey said:

thanks, that helped!

# November 17, 2009 6:06 PM

Elmer said:

The Handles way of doing things doesn't work because there are multiple buttons with the same ID. The Handles has no way of knowing to which button event to listen. Even more, buttons inside a repeater get a concatenated ID, something like RepeaterID_ButtonID because the repeater works as a NamingContainer. By telling each button in the repeater where its events must go and creating a Pubic event handler routine, you get around all these issues.

Hope this helps

# January 12, 2010 4:29 AM

christian said:

ha!

exactly what I was looking for, thanks, nanette!

# March 16, 2010 6:44 AM

Danish said:

Thanks

# August 2, 2010 3:52 AM

Kev said:

THANKS!!!!!!!!!!!!!

# August 19, 2010 10:16 AM

Raj said:

This Helped, Thank you

# October 28, 2010 6:02 PM

Jay said:

Thanks a Lot!!!!!!!!!!!

Really Cool Article

# April 15, 2011 8:13 AM

Naveen said:

This was a big help. Thanks Nannette.

These gotchas take forever to figure out on our own. Not to mention the accompanying stress and scratching our heads untill we bleed.

probably saved many people from this

# August 9, 2011 3:18 PM

maharu said:

God bless you.

I could not believe that it is working

tried for 3 hours to find a solution !!

# December 11, 2011 10:48 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)