- I have a dropdownlist with options “1”, “2”, and “3”.
- The user selects an option and presses a button.
- Based on what number they chose, 1, 2 or 3 textboxes will be created dynamically with IDs textBox_1, textBox_2, textBox_3 (for example).
- The user will then input into each textbox and press another button and the values of these textboxes are validated and saved.
Could someone please provide code on how to do this (seemingly very simple) task? I have asked two lengthy questions already on my actual project, that seem to have been misunderstood or I have gotten the wrong end of the stick (including the creation of the textboxes on Page_Load/Page_Init use of repeaters etc).
This needs to be done dynamically because in my project there are many controls and this number can vary. I can post my code so far if needs be, but in the other questions this seems to have confused matters.
One question: what if the controls are already created, should it be possible to create more than the available number in the dropdown(f.e. 3 +3)?
Assuming you allow to create more than the number of controls, you need to
ClickeventtextBox_4)ViewStateorSessionvariablePage_InitorPage_Load(at the latest) with the same ID as beforeThen you’ve ensured that ViewState gets loaded and events are triggered.
Here’s code which demonstrates what i mean:
load dynamic control from a dropdownlist event handler, how to preserve the control after button event