Solved

Repeater Command Not being executed

Posted on 2004-10-27
1,097 Views
Last Modified: 2008-03-06
Hello,
 I have a repeater with a button

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

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

CodeBehind

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

      Response.Redirect("http://www.globalwindows.com")
      Response.Write(e.CommandArgument)

      Response.Write(e.CommandName)
      Response.Write(e.CommandSource)

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

Best
Chuck
0
Question by:charlesbaldo
    7 Comments
     
    LVL 15

    Assisted Solution

    by:Thogek
    Question:
    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
    0
     
    LVL 8

    Accepted Solution

    by:
    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.
    0
     
    LVL 3

    Assisted Solution

    by:sajay
    attach the event to the button instead..

    OnClick="ButtonClicked"
    0
     
    LVL 28

    Assisted Solution

    by:mmarinov
    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
    Regards!
    B..M
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    daffodils,

    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

    B..M
    0
     

    Author Comment

    by:charlesbaldo
    Where should I add the code

    AddHandler button.Click, AddressOf Me.Button_Clicked

    in the Page_Load??

    Thank You
    chuck
    0
     
    LVL 28

    Expert Comment

    by:mmarinov
    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

    B..M
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now