Riki
11/19/2007 12:10:00 PM
D.Dark wrote:
> I've seen alot of questions and replies about building controls
> dynamically in ASP.net but i can't find one with answers to my
> specific situation:
>
> I'm trying to build controls dynamically based on the user selection
> from a DropDownListBox. I can't automatically build the controls in
> the Page_Init method from the start since I have to wait for the user
> to choose and option from the DropDown. Also, when the user chooses
> an option from the DropDown it posts back and immediately runs the
> Page_Init method before the SelectedIndexChanged for the DropDown
> (which I need to run to perform the processing necessary to determine
> how many controls needs to be created). And by the time it gets to
> the SelectedIndexChanged method to do the calculations, the Page_Init
> has already run. In addition to that, when the user changes their
> choice in the DropDown and it posts back, the Page_Init is seeing the
> results of the processing from the last selection since the
> SelectedIndexChanged hasn't run yet to recalculate based on the new
> selection.
>
> Hopefully someone out there has had a similar experience and can
> provide so insite, Thanks!
Your observations are correct.
In my opinion, the best solution to this is to wait until
SelectedIndexChanged, and do all the work there.
If necessary, delete all the controls that have been created in Page_Init,
and start again.
(You can put your controls in a placeholder control and run
myplaceholder.Controls.Clear() to delete them all).
I admit that this will be a little bit slower, because some of the work will
be done twice, but on the bright side, you avoid a lot of trouble.
--
Riki