Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding ASP Textbox dynamically
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
00817593
Message ID:
00818237
Views:
28
Paul,
What you suggested (and just read over without thinking the first time) turned out to be the solution. Thank you very much.
Chris (if you are reading),
Thank you for confirming to me that the example in the aps book worked. If not for you I was about to give up.
I appreciate your help.

>>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)
>
>
>// Instead of txtHiddenCount, you might be able to use your strFieldNum
>// I'm not sure what the rest of your code looks like
>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 );
>}
>
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Reply
Map
View

Click here to load this message in the networking platform