[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

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.
0
EliasSportsBureau
Asked:
EliasSportsBureau
1 Solution
 
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
 
boulder_bumCommented:
PS- Page.Load will be called before a PostBack event.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now