Saving a form when a button is generated

I am trying to insert form fields into a dynamically generated button event. The form is also generated dynamically. However, the send event handler does not start. Here is my code:

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
source share
1 answer

You need to create, add and connect an event in the OnInit Page method. After that, it will attach the control to the message and return the event.

 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 
+4
source

Source: https://habr.com/ru/post/1491952/


All Articles