动态按钮单击表格

时间:2015-07-06 14:22:24

标签: asp.net vb.net button dynamic

创建动态按钮并将其链接到所有创建的按钮的同一事件。

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

CancelOrder_click事件永远不会触发。我看到有人说它需要受到保护,所以我尝试了它,它仍然无效。

1 个答案:

答案 0 :(得分:1)

必须将

NewOrdersList正确地重新初始化为每次回发的总计数。它存放在哪里,ViewStateSessionHiddenfield

  

是的,我删除了我从会话中拉出List的部分...这是我在Page_Load上调用的方法所以应该调用它   对于所有实例,页面都已加载;包括回发。

尝试将代码从Page_Load移至Page_Init。前者对于这些事件来说可能为时已晚。