4

我正在尝试在动态生成的按钮事件上插入表单字段。表格也是动态生成的。但是,提交事件处理程序没有触发。这是我的代码:

Protected Sub BindForm()
    'select query for fetching record from database
    'dynamically generated button

    Dim btnSubmit As New Button()
    btnSubmit.ID = "btnSubmit"
    btnSubmit.Text = "Save"
    AddHandler btnSubmit.Click, AddressOf Me.btnSubmit_click
    form1.Controls.Add(btnSubmit)

End Sub

' Dynamic button click event
Protected Sub btnSubmit_click(ByVal sender As Object, ByVal e As EventArgs)
      Dim Query As String = "INSERT INTO table column values some_value"
End Sub
4

1 回答 1

4

您需要OnInitPage. 之后,它将在回发时绑定控件并触发事件。

Protected Overrides Sub OnInit(e As EventArgs)
    Dim btnSubmit As New Button()
    btnSubmit.ID = "btnSubmit"
    btnSubmit.Text = "Save"
    AddHandler btnSubmit.Click, AddressOf Me.btnSubmit_click
    form1.Controls.Add(btnSubmit)
End Sub 'OnInit
于 2013-07-17T18:28:17.087 回答