By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,081 Members | 862 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,081 IT Pros & Developers. It's quick & easy.

Problem catching click event

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You need to add the event to the button manually, something like this:
AddHandler myButton.Click, AddressOf myButton_Click
"Kjell Kristiansson" <kj******@hotmail.com> wrote in message
news:yw*********************@newsc.telia.net...
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

Nov 21 '05 #2

P: n/a

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

"Robin Tucker" <id*************************@reallyidont.com> wrote in
message news:d5*******************@news.demon.co.uk...
You need to add the event to the button manually, something like this:
AddHandler myButton.Click, AddressOf myButton_Click
"Kjell Kristiansson" <kj******@hotmail.com> wrote in message
news:yw*********************@newsc.telia.net...
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


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.