Getting data from dynamically created TextBoxes

I am dynamically creating TextBoxes onto an ASPX page and I need to store the data from these textboxes into a SQL database when a "Save" button is clicked.  The problem is that when I click the button a postback event occurs before the code in the click event.  During the postback all of the TextBoxes need to be recreated since they are dynamic and the data gets wiped out.  I have looked into order of events but nothing seems to work.  I am using C# as the language for the page.
EliasSportsBureauAsked:
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.

boulder_bumCommented:
When re-creating the TextBoxes on PostBack, just make sure they have the same ID's and are placed into the same containers, etc. Doing so will cause the newly re-created TextBoxes to be associated with the proper ViewState information. If you'd like clarification, please let me know.
0
boulder_bumCommented:
For example, you can have a function like this:


private void CreateTextboxes( int numberToCreate )
{
     for( int i = 0; i < numberToCreate; i++ )
     {
         TextBox t = new TextBox();
         t.ID = "txt" + i.ToString();
         //use whichever container you want: doesn't have to be Page
         Page.Controls.Add( t );
     }

     ViewState["TextBoxNum"] = numberToCreate;
}

You can call the function wherever you want to first create the TextBoxes, then on PostBack, do this:

//page load
//make sure you do this INSTEAD of the original instantiation, i.e. don't create them twice

int numToReCreate = (int)ViewState["TextBoxNum"];
CreateTextBoxes( numToReCreate );
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
boulder_bumCommented:
PS- Page.Load will be called before a PostBack event.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

ihenryCommented:
Have a look at this PAQ
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21212700.html

PS: I believe syntax isn't a problem for you. But let me know if you really need the code to be in C#.
0
gbelken99Commented:
I believe you problem is when you create the textboxes dynamically at runtime in the page_load event and when you click the save button it recreates these textboxes and erases your data try doing this

if page.ispostback = false then
     'Create your textboxes dynamically
end if

this will not overwrite your textboxes and allow you to get their values
0
boulder_bumCommented:
Actually, gbelken, you MUST re-create the dynamically created controls on each PostBack so they associate themselves with the ViewState though you wouldn't, of course, want to re-initialize the Text property.
0
EliasSportsBureauAuthor Commented:
Thanks boulder bum.
0
boulder_bumCommented:
Happy to help. :-)
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.