Click here to Skip to main content
16,016,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Hi,
i want to create a dynamic number of textboxes
i used this code
C#
TextBox box = new TextBox();
box.Text = "aa" + i.ToString();
box.ID = i.ToString();
form1.Controls.Add(box);

but it creates the textbox one time only
what i have to do to create as much as needed ? i am using asp.net c#
Posted
Updated 25-May-13 1:33am
v2

1 solution

Surround it with a loop. For example, if you want ten text boxes:
C#
for (int i = 1; i <= 10; i++)
    {
    TextBox box = new TextBox();
    box.Text = "aa" + i.ToString();
    box.ID = i.ToString();
    form1.Controls.Add(box);
    }
But you should really, really be doing this within the form1 class and instance - not outside it. Let a class take care of it's own content!
 
Share this answer
 
Comments
Salah Abualrob 25-May-13 7:41am    
but if the needed number is unknown ?
Maciej Los 25-May-13 7:50am    
What you mean "number (of textboxes) is unknown"?
You can replcae 10 with variable, for example:
int j = (get number of textboxes);
for (int i = 1; i <= j; i++)
...
Maciej Los 25-May-13 7:50am    
+5!
Salah Abualrob 25-May-13 14:28pm    
if i want div to be created on button click ?
i tried as i mentioned above, but it is created once each time i click the button, just one created whatever times i pressed !
OriginalGriff 25-May-13 15:35pm    
Just a guess: remove the semicolon at the end of the for loop line....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900