How do I retain values of dynamic controls after post back?

My application creates a number of dynamic textboxes at runtime based on the user's selection from a drop down list. I want to enter the values for these textboxes into a database, so I need to get the values the user enters. I have successfully found the dynamic controls using the findcontrol method after the user clicks submit. The problem is that the values that the user enters disappears when the page posts back to itself. How to I make the values of the dynamically created textboxes persist through the page post back so I can enter them into the database?
LVL 2
CitySecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the_billCommented:
Hi,
which page event are you getting the values? When you create your controls, you need to set EnableViewState to true
0
CitySecAuthor Commented:
I'm getting them in the click event of the button. The whole form is in an ajax updatepanel. I think what's happening is that it's getting the original value of the textbox (which was blank) and it's not seeing that the user entered a new value. I tried the enableviewstate with no luck. I ran a test with two scenarios. 1) I put 2 textboxes directly on the page in the HTML source code, and 2) I added the two textboxes at runtime in the codebehind. The first scenario worked fine but the second one did not. Is there something different in the page lifecycle when adding controls at runtime than adding them in the source that I'm forgetting about?
0
mohammedrafiqrajaCommented:
Try this code

Change extension txt to aspx

Mohammed Rafi
DynamicTextBox.txt
DynamicTextBox.aspx.vb
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CitySecAuthor Commented:
I tried that code and it worked, but it is doing one thing differently from what I am doing. Yours creates the textboxes on page load, and mine creates them during the selectedindexchanged event of a dropdownlist. When I move it to the page load I get the correct values upon form submission, but when I put it in the dropdownlist.selectedindexchanged event I don't get the correct values upon form submission - I get the old default values instead of the new values the user entered. Can you help me with this?
0
CitySecAuthor Commented:
I think the problem is the dropdownlist gets its selectedindexchanged on each page postback, and thus the textboxes are getting re-created when the submit button is being hit and populated with the default original values.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.