AddHandler and dynamically generated controls

G

Greg

Hello,

I have a form where, on a button's click event, I add some link buttons to
the page. I have an event handler for these link buttons' click event, which
I wire up to the buttons using AddHandler.

The problem is, I can't get the event handler to fire. When I click on one
of the link buttons, the link buttons vanish from the page, and the click
event handler is never fired.

I'm not sure if I'm putting AddHandler in the wrong place (I've tried the
routine where I generate the link buttons, as well as every event on Page and
the link buttons' parent control - a PlaceHolder - that seemed even remotely
likely) or if there's something else entirely that I'm missing. Any help
with getting Click to fire for these link buttons would be greatly
appreciated.

Thanks you!
Greg
 
G

Greg

Well, I seem to have answered my own question.

For some reason, controls keep track of all property changes you make
between requests, but they DON'T keep track of controls that you add to their
Controls collection. So basically, when I clicked one of the LinkButtons I
had created in the code previously, it did a postback, but it didn't fire the
Click event, because the control no longer existed! This also explains why
the LinkButtons all vanished from the page after the postback.

So to get around this, I had to keep track of the link buttons I had created
using the ViewState. Then, on postback, I had to check the ViewState to see
if I'd created any link buttons previously. If so, I had to recreate them,
and link the Click event to the event handler again with AddHandler. And
this worked!
 

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,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top