dynamic buttons
Posted: 5/1/2005 7:53:53 PM
By: Comfortably Anonymous
Times Read: 2,231
0 Dislikes: 0
Topic: Programming: .NET Framework
I have tried to do something similar to your dynamic button code, but when I click on the button I get posted back without calling the ButtonHandler.  Can you tell me why?

Thanks.
Rating: (You must be logged in to vote)
Discussion View:
Replies:

dynamic buttons
Posted: 5/1/2005 7:53:53 PM
By: Comfortably Anonymous
Times Read: 2,231
0 Dislikes: 0
Topic: Programming: .NET Framework
Can you post some example code I can take a look at?
Rating: (You must be logged in to vote)

dynamic buttons
Posted: 5/1/2005 7:53:53 PM
By: Comfortably Anonymous
Times Read: 2,231
0 Dislikes: 0
Topic: Programming: .NET Framework
A big thing that causes 'dead postbacks' when using dynamic buttons is that ASP.NET is a bit 'stupid' when it comes to these dynamic buttons and forgets about them unless they are regenerated with EACH postback. Quite a pain to deal with it, can lead to some strangely formatted code to make sure the buttons are generated with each postback, especially when they are generated only in certain sections of the code. If the button is not re-generated before the event handler piece is called, then ASP.NET has no remembrance of the button from the previous page load, and doesn't know which event handler to connect the event to, which seems kind of screwy, but that's how it's currently working in .NET 1.0 and 1.1. Haven't played with 2.0 yet, but it may be addressed there, I've heard rumors of it.

(I'm wondering right now about experimenting with setting EnableViewState = True for a dynamically-generated control - does that make ASP.NET remember it? I'll have to try it, although I've got a sinking feeling in the back of my mind that I tried it sometime last year and it failed. I'll go ahead and try it and post back my results.)

Dynamically generated controls MUST be re-generated with each postback or
Either have the buttons generated in the Page_Load section, and outside of a !(IsPostBack) section, or have them generated in a separate function that is always called on postback.

Page_Load is always invoked prior to the event handler(s) being called. If you generate/re-generated any dynamic controls there, then ASP.NET has no problems connecting to the event handler.

I'm betting that's your issue.
Rating: (You must be logged in to vote)