Dynamically Building Controls in ASP.net

D

D.Dark

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!
 
R

Riki

D.Dark said:
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.
 
D

D.Dark

Thanks for the help. I was already using a placeholder for the controls so i
could clear them out all at once but I didn't want to have to create a
predetermined number of controls just to make sure that they were always
there and then remove the ones that I didn't need later.

I ended up using a control array to solve my problem. With the control array
I only created one control in the Page_Init and created the rest of the
controls in the SelectedIndexChanged event. Since they were all part of the
same control array with the same name, all the events for the controls wired
up properly even though they weren't created in the Page_Init. Then I used
ViewState to keep track of the number of controls that were created the last
time so I could recreate the same number of controls again when the page
posts back.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top