>I have been struggling with this problem all day yesterday and hope someone might suggest a solution or a web site to where solution might be.
>
>I want to dynamically add a ASP textbox to a Placeholder when user clicks on a button.
>
>The following code:
>
>
>txtTextBox = new TextBox();
>txtTextBox.ID = "txtProduct" + strFieldNum;
>PlaceHolder1.Controls.Add( txtTextBox );
>
>
>creates one textbox (and not ASP textbox). On the next button click, even though the value "strFieldNum" is incrementing, the same textbox just gets refreshed. How can I change the code to make it ASP textbox and to make it add new one every time.
>
>Any suggestions would be greatly appreciated.
When your page is rendered, remember it's using the page as-is before your code is run (the new control you added isn't persisted between hits). Your initial page doesn't have a textbox in the placeholder, therefore when the code above runs, you're only adding one new textbox. You will need to do something like add a hidden form variable or session variable that contains the number of times your "Add textbox" code has been called. That way, when it gets called again, you can query this variable, add one to it, update the hidden variable again, then run a loop to add all the new textboxes.
Something like this (I didn't test this code, so you might have to fix it before it works)
int iTextboxCount = int.Parse(Request.Form["txtHiddenCount"]);
for (int iLoop = 1; iLoop <= iTextboxCount; iLoop++)
{
TextBox txtTextBox = new TextBox();
txtTextBox.ID = "txtProduct" + iLoop.ToString();
PlaceHolder1.Controls.Add( txtTextBox );
}