Work and Play

Tuesday, December 20, 2005

More Fun With Dynamic Controls

I thought I was done with this, but apparently not :). I decided (after my earlier post) not to dynamically load user controls on the project I'm working on, simply because it's almost complete and it will take too much time to tear them apart and put them on dynamically. Unfortunately, the only way I was able to correct the callbac lifecycle problem was by creating a flag, and only processing the prerender method on a callback after the callback event had been handled.

Anyhow, here was the situation today: my dynamically stored server controls were not always being loaded correctly at postback. I was doing testing today to make sure all of the components were working when I found this error. In addition, this was on a page which was not using callbacks, just standard ASP.NET 1.1 controls.
After some fun debugging, I found that the dynamic control had good values in it after the load method completed, but it was reverted to bad values again at PreRender. Part of the problem here is that I'm developing a wizard-style app, so on a postback I need to load the old control, populate the values, and then load the new control. Depending on which page is using this user control, the reload can be either the same information or a completely different control.
My first impression was to make sure I load the data first thing on PreRender. However, that is a very easy rule to forget, and difficult to debug later on if/when we reuse this user control. Next idea was just to create an event, so that when the control contains the postback data the event is fired and anyone who needs that data can handle it.
The event is what I went with, but unfortunately it was tricky. The event would have to be created after Load has completed (unfortunately overriding OnLoad or LoadViewState after they have processed events does not work). I had to implement IPostBackDataHandler and use RaisePostBackDataChangedEvent to be able to fire my event at the correct time. To get this to work you need to use Page.RegisterRequiresPostBack(this); in your OnInit. Too bad this project isn't in 2.0 - the extra events supplied by the page would have been very useful in handling this situation.

0 Comments:

Post a Comment

<< Home