get value from the dynamic create textbox

190 views Asked by At

I am quite new in asp.net. I am wonder how can I get the value of the textbox that create dynamically from C#.

this code is the way i create the textbox from page load event...

for( int i =0; i<30; i++){                
                TextBox txt = new TextBox();
                txt.Text = "ASDASDASD";
                txt.ID = "txt - " + i.ToString(); 
                data.Controls.Add(txt);
    }

I wonder how can I get the value of the text box in button_click event. I tried string test = "txt - " + i.ToString(); but I unable to get it.

thanks for the help.

2

There are 2 answers

8
Cyral On BEST ANSWER

Create an array or list of textboxes:

private TextBox[] textBoxes = new TextBox[30];

And assign a new textbox to each position:

for(int i =0; i<30; i++){                
      TextBox txt = new TextBox();
      txt.Text = "ASDASDASD";
      txt.ID = "txt - " + i.ToString(); 
      textBoxes[i] = txt;
      data.Controls.Add(txt);
}

To get the value of any textbox, do:

string value = textBoxes[i].Text;
1
Jankya On

Try this.

String textvalue = String.Empty;
for(int i=0;i<= data.controls.count-1;i++)
{    
 //Specify the Index if u have 
  if(index == i)
     {
          Textbox txt = data.controls[i] as Textbox;
          textvalue  = txt.text;
          return;
     }
}