Search code examples
asp.netvb.netonclickrepeateritemcommand

How to fire a button event from inside a repeater?


I have done my research but can't find an efficient way to do the following in VB:

  • Each button should fire the same event.
  • The button event saves every repeater item and so each event is not unique.

I am aware I can use the ItemCommand option but have not been able to get it working as desired.

ASP.NET

Inside Repeater Item

<asp:Button ID="btnSave" RunAt="Server"/>

VB.NET

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    sqlConn.Open()
        For Each Item As RepeaterItem In rpt.Items
        ...
        Next
    sqlConn.Close()
End Sub

Solution

  • Edit:

    After some research here on SO, I found that others events than ItemCommand are not caught by Asp:Repeater, as FlySwat said on his answer. So you'll need to write your VB.NET code like this:

    First, declare the ItemCommand event on your page with something like this:

    Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand
        If e.CommandName = "Save" Then
            'Save
        End If
    End Sub
    

    Then, on Asp:Button markup inside the Asp:Repeater, you must set its CommandName property like this:

    <Asp:Button ID="btnSave" runat="server" CommandName="Save" UseSubmitBehavior="false"/>
    

    Take a look here to learn more about the UseSubmitBehavior.

    Try it.