Re: dynamic buttons
Posted: 6/6/2005 9:56:19 AM
By: Comfortably Anonymous
Times Read: 1,877
0 Dislikes: 0
Topic: Programming: .NET Framework
Parent Message
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)
Discussion View:
Replies:

Re: dynamic buttons
Posted: 6/6/2005 9:56:19 AM
By: Comfortably Anonymous
Times Read: 1,877
0 Dislikes: 0
Topic: Programming: .NET Framework
I'm also having trouble with this - I get the impression you not only have to recreate the buttons, but you have to place them as well.  This is a real pain if the event handler is is intended to change the layout of the page, e.g. not include a button that was there before the postback.  I'd be happy to recreate all buttons from pre-postback so they're tied to their handlers, but I don't want to have to place them.

In a simple case, I could just make the button invisible, but that's not an option if it's to be placed inside another control whose existence is conditional.  e.g. initially a button sits in a cell of a row of a table.  Which is inside a panel, etc...  The button text is "Remove all this stuff!".  After postback & event handling the table is not to be drawn.  How should such a button be handled?

Any ideas?

John S
Rating: (You must be logged in to vote)