2

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
Damon
  • 3,004
  • 7
  • 24
  • 28
DreamTeK
  • 32,537
  • 27
  • 112
  • 171

2 Answers2

9

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.

Community
  • 1
  • 1
aledpardo
  • 761
  • 9
  • 19
  • Yes I had already tried that although I had not added the with events. Still this seems not to be working for my situations. Thanks for the assistance though. – DreamTeK Dec 12 '13 at 11:15
  • But, what happens? The click event is not being fired? – aledpardo Dec 12 '13 at 11:20
  • Well, after some research on SO, I found [this question](http://stackoverflow.com/questions/42396/asp-net-why-is-my-buttons-click-command-events-not-binding-firing-in-a-repeate). There [FlySwat](http://stackoverflow.com/users/1965/flyswat) explains that the only `Event` that can be raised inside an `Asp:Repeater` is the `ItemCommand`. Please, take a look. I'll edit my answer. – aledpardo Dec 12 '13 at 11:36
  • Usually, we use CommandArgument to store some value, so it's implying on the event hasn't being fired. How the page is behaving? Is it posting back? What's happening when you debug? – aledpardo Dec 12 '13 at 12:23
  • Try to set the `UseSubmitBehavior="false"` `Button`'s property. I think it will work now. – aledpardo Dec 12 '13 at 12:42
  • And it was as simple as that. Many Thanks for your efforts Alexandre. – DreamTeK Dec 12 '13 at 13:46
5

When the button is inside a Repeater template, you need to add OnClick event, you can add event on ItemDataBound event of the Repeater control.

Your .aspx code will look something like this:

 <asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:Button  ID="btnSave" runat="server" Text="SomeText" />
    </ItemTemplate>
</asp:Repeater>

code-behind

void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == Repeater1.AlternatingItem || e.Item.ItemType == Repeater1.Item)
    {
        var btn = e.Item.FindControl("btnSave") as Button;
        if (btn != null)
        {  // adding button event 
            btn.Click += new EventHandler(btn_Click);
        }
    }
}

void btn_Click(object sender, EventArgs e)
{
 //write your code 
}

in vb.net

Private Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
    If e.Item.ItemType = Repeater1.AlternatingItem OrElse e.Item.ItemType = Repeater1.Item Then
        Dim btn = TryCast(e.Item.FindControl("btnSave"), Button)
        If btn IsNot Nothing Then
            ' adding button event 
            btn.Click += New EventHandler(btn_Click)
        End If
    End If
End Sub

Private Sub btn_Click(sender As Object, e As EventArgs)
    'write your code 
End Sub
Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31
  • Thanks Sain but as I mentioned above I have done my research. I found this example on google already and I needed a vb solution I'm not very good with c#. – DreamTeK Dec 12 '13 at 10:32