I want to create 16 Textboxes, named textbox1 to textbox16, inside a for loop which runs for 16 times. Hence, 1 textbox is created in each loop. How to achieve this ?
2 Answers
You can't make dynamically named variables. In this kind of situationen, it makes most sense to keep the controls in some collection, for instance in a List<T>:
List<TextBox> textBoxes = new List<TextBox>();
for(int i = 1 ; i <= 16 ; i++ )
{
var tb = new TextBox() { Name = "textbox" + i };
textBoxes.Add(tb);
}
Comments
Try this :
for(int counter=0;counter<16;counter++){
TextBox TB = new TextBox();
TB.Id = "textbox" + (counter + 1);
// code to add this textbox in screen
}
2 Comments
John Watson
how will i access individual textbox ?
Kundan Singh Chouhan
You will create any kind of placeholder in the screen and add these textboxes into it. You can find these controls via FindControl Method.
Nameof theTextBoxinstead?