Thursday, March 29, 2012

Please explain why AddHandler works in Page_Load, but not Button_C

I need to understand why if I add a control and use AddHandler to connect it
s
click event, it will work in Page_Load, but not in a Button_Click.
The idea is that the user types some data, presses the button, gets a list
of results (each with a LinkButton) and can then press one of the link
buttons to get further information. The newly added link buttons appear, but
the click event added with AddHandler does not fire.
A control added in Page_Load with an event handler added with AddHandler
works fine.
I have not writted a web app previously and I guess that I do not understand
how dynamically added handlers work (or dont work).
John Austinthe key to understanding asp.net is to know that its stateless, and a form
instance is only used for one request. so if you add a link in a button
click, and the user clicks on that link, a new form is built to process that
request. your code must know to add the handler when that happens. usually
you'd store a flag in viewstate or session to known when to add the handler
(you must also re-add the link control).
-- bruce (sqlwork.com)
"John Austin" <John.Austin@.nospam.nospam> wrote in message
news:08B9C89B-CB28-41CE-899E-4AF85F704B84@.microsoft.com...
>I need to understand why if I add a control and use AddHandler to connect
>its
> click event, it will work in Page_Load, but not in a Button_Click.
> The idea is that the user types some data, presses the button, gets a list
> of results (each with a LinkButton) and can then press one of the link
> buttons to get further information. The newly added link buttons appear,
> but
> the click event added with AddHandler does not fire.
> A control added in Page_Load with an event handler added with AddHandler
> works fine.
> I have not writted a web app previously and I guess that I do not
> understand
> how dynamically added handlers work (or dont work).
> --
> John Austin
It has to do with the order of events in .NET. By the time you hit button
events, you have already configured controls. Under "sender", you can find
out which button was clicked and adjust appropriately in Page_Load. Be
careful, however, with overloading Page_Load with too much, as you can end
up with disaster.
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
http://gregorybeamer.spaces.live.com
****************************************
*********
Think outside of the box!
****************************************
*********
"John Austin" <John.Austin@.nospam.nospam> wrote in message
news:08B9C89B-CB28-41CE-899E-4AF85F704B84@.microsoft.com...
>I need to understand why if I add a control and use AddHandler to connect
>its
> click event, it will work in Page_Load, but not in a Button_Click.
> The idea is that the user types some data, presses the button, gets a list
> of results (each with a LinkButton) and can then press one of the link
> buttons to get further information. The newly added link buttons appear,
> but
> the click event added with AddHandler does not fire.
> A control added in Page_Load with an event handler added with AddHandler
> works fine.
> I have not writted a web app previously and I guess that I do not
> understand
> how dynamically added handlers work (or dont work).
> --
> John Austin

0 comments:

Post a Comment