Problem catching click event

  • Thread starter Kjell Kristiansson
  • Start date
K

Kjell Kristiansson

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
 
R

Robin Tucker

You need to add the event to the button manually, something like this:


AddHandler myButton.Click, AddressOf myButton_Click
 
K

Kjell Kristiansson

Forgot to mention - but I have tested that too. Does not help.

I start to fear that this is by design ;-(

It seem like the control (the button myButton) has to be hooked into the
"webForm" hierarchy to receive the event. I have not tested that yet as that
is against the design.

Can anyone confirm this?
Is there any simple work-around?

Kjell K
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top