event handling for dynamic controls

P

Patrick

I have a web page with a table that i generate rows for when a database query is run. Each row contains a button column and I would like to add a handler for the click event of the button created at run time. I tried the following code

...
Dim OKButton As Button = New Butto
AddHandler OKButton.Click, AddressOf AcceptButton_Clic
' add the button to the cel
NewCell.Controls.Add(OKButton
' add the cell to the row of the tabl
NewRow.Cells.Add(NewCell
...

and here's the handler

Private Sub AcceptButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs
...
End Su

I am never getting into that handler code. Is what I am trying even possible?
 
A

Alessandro Zifiglio

add the handler after the control has been dynamically added

Dim OKButton As Button = New Button
' add the button to the cell
NewCell.Controls.Add(OKButton)
'Now is a good time to add your handler
AddHandler OKButton.Click, AddressOf AcceptButton_Click
' add the cell to the row of the table
NewRow.Cells.Add(NewCell)
Patrick said:
I have a web page with a table that i generate rows for when a database
query is run. Each row contains a button column and I would like to add a
handler for the click event of the button created at run time. I tried the
following code:
...
Dim OKButton As Button = New Button
AddHandler OKButton.Click, AddressOf AcceptButton_Click
' add the button to the cell
NewCell.Controls.Add(OKButton)
' add the cell to the row of the table
NewRow.Cells.Add(NewCell)
...

and here's the handler:

Private Sub AcceptButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
...
End Sub

I am never getting into that handler code. Is what I am trying even
possible?
 
P

Patrick

thanks for the response...unfortunately, that's not working. some other details:

* i am using a code behind class to handle the events on the form
* a new row is added to the table in response to a click event of a static button
 
A

Alessandro Zifiglio

Patrick, just tested, the code i posted works. One reason why it is not
working is that you are adding dynamic controls so makesure they are rebuilt
even after postback.
If you are not rebuilding it after postback the event wont fire. Any dynamic
control you add on your page needs to be rebuilt after postback, so remove
any ispostback check on your page and make sure the same code you used to
add the dynamic control runs after postback
Patrick said:
thanks for the response...unfortunately, that's not working. some other details:

* i am using a code behind class to handle the events on the form
* a new row is added to the table in response to a click event of a static
button
 
A

Alessandro Zifiglio

oops, typo corrections. I meant to say *I tested the code and it works*
so it could be because you are not not rebuilding your dynamically added
controls after postback . . .
 
P

Patrick

thanks Alessandro...i have decided to use the DataGrid and have been able to do so with success .. so far :)
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top