• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1112
  • Last Modified:

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 Baldo
Charles Baldo
4 Solutions
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: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsrepeaterclassonitemcommandtopic.asp
Repeater.ItemCommand event: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsrepeaterclassitemcommandtopic.asp
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.
attach the event to the button instead..

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

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.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now