473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamically-added Button not triggering event handler. Why?

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

Mar 15 '07 #1
4 1874
On Mar 15, 10:40 am, "parsifal" <sean.gilbert...@gmail.comwrote:
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

Mar 15 '07 #2
On Mar 15, 4:40 pm, "parsifal" <sean.gilbert...@gmail.comwrote:
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.

Mar 15 '07 #3
On Mar 15, 11:27 am, "Alexey Smirnov" <alexey.smir...@gmail.com>
wrote:
On Mar 15, 4:40 pm, "parsifal" <sean.gilbert...@gmail.comwrote:


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.- 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?

Mar 15 '07 #4
On Mar 15, 5:38 pm, "parsifal" <sean.gilbert...@gmail.comwrote:
On Mar 15, 11:27 am, "Alexey Smirnov" <alexey.smir...@gmail.com>
wrote:


On Mar 15, 4:40 pm, "parsifal" <sean.gilbert...@gmail.comwrote:
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.- 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?- 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.
Mar 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: john_williams_800 | last post by:
Hi; I am just starting to use the DOM to do some more advanced javascripting so please be patient with my question if it is an ignorant question. I would like to use the DOM to dynamically...
7
by: Fabian Wauthier | last post by:
Hi list, I am trying to dynamically grow a 2 dimensional array (Atom ***Screen) of pointers to a struct Atom (i.e. the head of a linked list). I am not sure if this is the right way to do it: ...
2
by: Laura | last post by:
In C#, the method was dynamically invoked through reflection. But How do we dynamically cast the return object to its own type? Here is the sample code: // dynamically invoke the method Object...
1
by: Reza Nabi | last post by:
Bakground: I have a webform (LoadCtl.aspx) which loads the user control to a placeholder dynamically based on the ctlName querystring passed in the URL. Webform (LoadCtl.aspx) also passes a...
5
by: Angel | last post by:
Is there a way to create an IFRAME dynamically via VB.NET. In other words creating the HTML element in the server side code? thanks in advance....
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
2
by: Smithers | last post by:
Using 3.5, I am stuck in attempting to: 1. Dynamically load an assembly 2. Instantiate a class from that assembly (the client code is in a different namespace than the namespace of the...
11
by: Nadeem | last post by:
Hello all, I'm trying to write a function that will dynamically generate other functions via exec. I then want to be able to import the file (module) containing this function and use it in other...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.