views:

4102

answers:

5

Here's the code from the ascx that has the repeater:

<asp:Repeater ID="ListOfEmails" runat="server" >
    <HeaderTemplate><h3>A sub-header:</h3></HeaderTemplate>
    <ItemTemplate>
     [Some other stuff is here]
     <asp:Button ID="removeEmail" runat="server" Text="X" ToolTip="remove" />
    </ItemTemplate>
</asp:Repeater>

And in the codebehind for the repeater's databound and events:

Protected Sub ListOfEmails_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ListOfEmails.ItemDataBound
    If (e.Item.ItemType = ListItemType.Item) Or (e.Item.ItemType = ListItemType.AlternatingItem) Then
     Dim removeEmail As Button = CType(e.Item.FindControl("removeEmail"), Button)
     removeEmail.CommandArgument = e.Item.ItemIndex.ToString()

     AddHandler removeEmail.Click, AddressOf removeEmail_Click
     AddHandler removeEmail.Command, AddressOf removeEmail_Command
    End If
End Sub

Sub removeEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Write("<h1>click</h1>")
End Sub

Sub removeEmail_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
    Response.Write("<h1>command</h1>")
End Sub

Neither the click or command is getting called, what am I doing wrong?

A: 

Here's an experiment for you to try:

Set a breakpoint on ListOfEmails_ItemDataBound and see if it's being called for postbacks.

Brad Wilson
A: 

@Brad I tried setting a breakpoint in there and the repeater's itemdatabound works fine.

travis
+7  A: 

Controls nested inside of Repeaters do not intercept events. Instead you need to bind to the Repeater.ItemCommand Event.

ItemCommand contains RepeaterCommandEventArgs which has two important fields:

  • CommandName
  • CommandArgument

So, a trivial example:

  void rptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                // Stuff to databind
                Button myButton = (Button)e.Item.FindControl("myButton");

                myButton.CommandName = "Add";
                myButton.CommandArgument = "Some Identifying Argument";
            }
        }

void rptr_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                // Do your event
            }
        }

EDIT: Markdown totally hosed my indentation, oh well, I should stop writing code in this textbox anyways.

FlySwat
+1  A: 

You need to handle the ItemCommand event on your Repeater. Here's an example.

Then, your button clicks will be handled by the ListOfEmails_ItemCommand method. I don't think wiring up the Click or Command event (of the button) in ItemDataBound will work.

Craig
A: 

@Jonathan and Craig I just tried using the repeater's itemcommand and it works, thanks.

travis