Dynamically-added Button not triggering event handler. Why?

P

parsifal

Hi everyone :)

In my Page_Load, I create a control, and do an AddHandler on it
(VB.NET). The button is created and displayed, and does cause a
postback, but the event handler isn't fired. What am I doing wrong?
Here's the code for adding the button:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Using btn As New Button
btn.Text = "clicky!"
btn.CommandName = "CheckIn"
btn.CommandArgument = "stuff!"
AddHandler btn.Command, AddressOf CheckInAppointment

btn.ID = "StuffButton"

Debug.Controls.Add(btn)
End Using
End Sub

Thanks for any help!!
Sean
 
P

parsifal

Hi everyone :)

In my Page_Load, I create a control, and do an AddHandler on it
(VB.NET). The button is created and displayed, and does cause a
postback, but the event handler isn't fired. What am I doing wrong?
Here's the code for adding the button:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Using btn As New Button
btn.Text = "clicky!"
btn.CommandName = "CheckIn"
btn.CommandArgument = "stuff!"
AddHandler btn.Command, AddressOf CheckInAppointment

btn.ID = "StuffButton"

Debug.Controls.Add(btn)
End Using
End Sub

Thanks for any help!!
Sean

Okay, I figured out my problem. This is a subtle one.

If I take out the Using block, it works fine. I find this odd because
once I add it to the Controls array of the Debug Panel, shouldn't a
reference to the button be held? (This question assumes that the
problem is that the object becomes lost at some point, because I'm
using a Using block.) No other control I use has the drawback that I
can't use a Using block. But the event-handling portion must be
what's being broken somehow, because the button is still displayed.

Can anyone explain to me why this is the case? I'm very curious.

Thanks,
Sean
 
G

Guest

Hi everyone :)

In my Page_Load, I create a control, and do an AddHandler on it
(VB.NET). The button is created and displayed, and does cause a
postback, but the event handler isn't fired. What am I doing wrong?
Here's the code for adding the button:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Using btn As New Button
btn.Text = "clicky!"
btn.CommandName = "CheckIn"
btn.CommandArgument = "stuff!"
AddHandler btn.Command, AddressOf CheckInAppointment

btn.ID = "StuffButton"

Debug.Controls.Add(btn)
End Using
End Sub

Thanks for any help!!
Sean

Using block disposes all resources when the code exits the block.
Delete it.
 
P

parsifal

Using block disposes all resources when the code exits the block.
Delete it.- Hide quoted text -

- Show quoted text -

But if I add the "Used" object to a collection (or if anything else
maintains a reference to the Used object), doesn't it remain in memory?
 
G

Guest

But if I add the "Used" object to a collection (or if anything else
maintains a reference to the Used object), doesn't it remain in memory?- Hide quoted text -

- Show quoted text -


OK, I see you figured out the problem.

The Using statement defines a scope for an object that implement the
IDisposable interface where the object will be automatically exposed
at the end of the scope. The Button control inherited from the
WebControl and Control that implement the IDisposable.

So, using Using..End Using is the same if you call

btn.Dispose()

at the end of your block.
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top