我正在尝试在动态生成的按钮事件上插入表单字段。该表格也是动态生成的。但是,submit事件处理程序不会触发。这是我的代码:
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
最佳答案
您需要在OnInit
的Page
方法中创建,添加和连接事件。完成此操作后,它将绑定回发控件并触发事件。
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