0

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 ?

1
  • Why do you want to change the name of the variable in each iteration? Don't you want to change the Name of the TextBox instead? Commented Sep 15, 2012 at 17:40

2 Answers 2

2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

how will i access individual textbox ?
You will create any kind of placeholder in the screen and add these textboxes into it. You can find these controls via FindControl Method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.