I have not been able to figure out how to catch the click event from a
button that is added dynamically to a table. In priciple the process is as
follows:
When I first create the page I analyse data and if appropriate:
- add one or more row to a table in the webform
- add cells to the rows
- add a button (myButton) where the user can change some settings.
- enable/disable some other buttons to the form
This is all done in a function called within page_load.
myButton is declared as follows (global in the class):
Protected WithEvents myButton As System.Web.UI.WebControls.Button
and the function is declared:
Private Sub myButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles myButton.Click
When the page comes back (IsPostBack) the click event is not caught in my
function.
My guess :
I do not know if there will be any button until I am in the myButton_Click
so it has to be created there. Then as the button is not created when the
event is to be handled noone is there to handle it.
My tests that do not work:
Protected WithEvents myButton As New System.Web.UI.WebControls.Button
(Added New - does not work)
Created the following function called early in page_load
Private Sub NewPage()
' Here we set up what is needed to catch events etc
' =================================================
myButton= New Button
myButton.Text = "Change"
myButton.ID = "myButton"
myButton.Attributes.Add("Runat", "Server")
End Sub
Does not work as is and not with other ID and/or without the added
attributes
So how should ths be done?
Kjell K