How to access values of dynamically created TextBoxes
Posted
by
SAMIR BHOGAYTA
on Samir ASP.NET with C# Technology
See other posts from Samir ASP.NET with C# Technology
or by SAMIR BHOGAYTA
Published on 2011-09-08T20:01:00.001-07:00
Indexed on
2011/11/11
18:23 UTC
Read the original article
Hit count: 454
for(int i=0;i<10;i++) {
TextBox objBox = new TextBox();
objBox.ID = "objBox" + i.ToString();
this.Page.Controls.Add(objBox);
}
After PostBack, you want to retrieve the text entered in the third TextBox. If you try this:
String strText = objBox2.Text;
you'll receive an exception. Why? Because the boxes have not been created again and the local variable objBox2 simply not exists.
How to retrieve the Box?
You'll need to recreate the box by using the code above. Then, you may try to get its value by using the following code:
TextBox objBox2;
objBox2 = this.Page.FindControl("objBox2") as TextBox;
if(objBox2 != null)
Response.Write(objBox2.Text);
© Samir ASP.NET with C# Technology or respective owner