Dynamic Button Click in Table

Creating a dynamic button and linking it to the same event for all buttons created.

Private Sub PopulateNewOrders()
    Dim buttonNumber As Integer = 1
    For Each value In NewOrdersList
        Dim button As New Button
        button.ID = buttonNumber
        buttonNumber = buttonNumber + 1
        button.Text = "Cancel Order"
        button.CssClass = "CancelBtn"
        AddHandler button.Click, AddressOf CancelOrder_Click
        Dim Order As New TableRow
        Dim tempCell As New TableCell
        tempCell.Controls.Add(button)
        Order.Cells.Add(tempCell)
        CurrentOrderForm.Rows.Add(Order)
    Next
End Sub

Protected Sub CancelOrder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim button As Button = sender
        Dim int As Integer = Convert.ToInt32(button.ID)
        showButtonPressedID.text = int
    End Sub

The CancelOrder_click event never triggers. I saw someone saying it needed to be protected so I tried that and it still doesn't work.


ANSWERS:


NewOrdersList must be re-initialized correctly to the total-count on every postback. Where is it stored, in ViewState, Session or a Hiddenfield?

Yes, I removed the part where I pulled the List from the session ... This is a method i'm calling on the Page_Load so it should be called for all instances the page is loaded; Including postback.

Try to move the code from Page_Load to Page_Init. The former might be too late for the events.



 MORE:


 ? Create onClick event for dynamically created button asp.net
 ? Dynamically Add Click event to asp button
 ? Dynamically Add Click event to asp button
 ? Dynamically Add Click event to asp button
 ? Button Click Event Getting Lost
 ? Dynamically assign button events in c# asp.net
 ? how to create an asp button dynamically and add event to it
 ? C# how to handle multiple button click events?
 ? C# Add Controls To Panel In a Loop
 ? C# Add Controls To Panel In a Loop