Repeater Command Not being executed

 I have a repeater with a button

<asp:Repeater id=GalleryRepeater  OnItemCommand="GalleryRepeater_ItemCommand" runat="server">
<asp:Button id="btnGallery" OnCommand="GalleryClicked" Text='<%# DataBinder.Eval(Container.DataItem, "Gallery")%>' CommandName="Display" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Gallery")%>' runat="server"></asp:Button>

The Button seems to be woring ok. The Text, CommandName, and CommandArgument are binded nicely with data

The problem is the buttons evenet never seems to fire


Protected WithEvents GalleryRepeater As System.Web.UI.WebControls.Repeater
Protected WithEvents btnGallery As System.Web.UI.WebControls.Button

Public Sub GalleryRepeater_ItemCommand(ByVal sender As Object, ByVal e As RepeaterCommandEventArgs) Handles   GalleryRepeater.ItemCommand



End Sub

When the button is clicked the subroutine never seems to be called.  I got this same code to work in an inline version  of the code. I did a lot of cutting and pasting but think I got it right.

Any help is apprecited

Charles BaldoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Is the button's OnCommand="GalleryClicked" attribute serving any purpose?  (I.e., is there a GalleryClicked method?  If so, does it get called?)  If so, is there a reason to have both Repeater.OnItemCommand and Button.OnCommand specified (since the two would seem to conflict).

More about...
Repeater.OnItemCommand method:
Repeater.ItemCommand event:
I think your GalleryRepeater_ItemCommand() event handler is not wired to your Repeater control.

A simple example.. lets say you have a button control.
To wire your event handler to the Button, you must create an instance of EventHandler that takes a reference to Button_Clicked in its argument and add this delegate instance to the Click event.
So, you need something like this in your code:
AddHandler button.Click, AddressOf Me.Button_Clicked

You need to add a wiring for your Repeater control to its ItemCommand() event handler.

If you are using Visual Studio .NET, then all you have to do is double click the control in the design mode, and it will autogenerate this code for you.

>> I got this same code to work in an inline version  of the code
The reason this works in inline version is because the AutoEventWireup is true by default.. so an event handler like "control_event" gets automatically wired to a "control" 's  "event".

However in Visual Studio.. AutoEventWireup is false by default.. since it generates code for event handler wiring.
Now.. since you have copied and pasted code.. you can add 'event wiring':
 --- manually as in the above 'buttton" example
 --- or you can do it by double-clicking the control in design mode.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
attach the event to the button instead..

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Hi charlesbaldo,

1. Have you bind the repeater in
If Not IsPostBack Then
End If
If you not - the behaviour will be as you write

2. Set a breakpoint in the GalleryRepeater_ItemCommand and GallerClicked, debug the application and see if there will be a problem

when you put in the tag ( like in the code OnItemCommand="GalleryRepeater_ItemCommand" ) the event name and the method that will handle with it you don't have to add

AddHandler GalleryRepeater.ItemCommand, AddressOf Me.GalleryRepeater_ItemCommand

Charles BaldoSoftware DeveloperAuthor Commented:
Where should I add the code

AddHandler button.Click, AddressOf Me.Button_Clicked

in the Page_Load??

Thank You
if you want to add this handler you have to create itemdatabound event and there

If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.Alternating Then
 Dim btn as Button = CType(e.Item.FIndControl("btnGallery"), Button )
 AddHandler btn.Click, AddressOf Me.Button_Clicked
End If

but you can not access direct the btnGallery because it is nested control

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.