Click here to Skip to main content
15,887,947 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi all ,

I am able to add textbox on page at run time dynamically using code like:-
Textbox objtxtBox = new Textbox();
objtxtBox.name = "txtFName";


I am able to display new textbox on button click but i want to get the value of that textbox on the click o another button, but unable to get the value using same textbox name property.


how to do this.
Posted
Updated 31-Jul-11 7:37am
v2

Did you add the control to the Form.Controls collection after you created it?
 
Share this answer
 
You can get your text box value by the following code:

Control cc = this.Controls.Find(txtFName, true).First();
string value = cc.Text;


Hope be helpful
Theingi Win
 
Share this answer
 
Might be helpful,

C#
private void btnCreate_Click(object sender, EventArgs e)
{
     TextBox txtBox = new TextBox();
     txtBox.Name = "txtFName";
     txtBox.Text = "Hello world";
     Controls.Add(txtBox);
}
private void btnFind_Click(object sender, EventArgs e)
{
     TextBox rc = (TextBox)FindDynamicControlByName("txtFName");
}
private Control FindDynamicControlByName(string controlName)
{
      return Controls.Find(controlName, true).FirstOrDefault();
}


For Ref:
Control.Controlcollection.Find[^]

:)
 
Share this answer
 
Comments
suniltikli 1-Aug-11 14:02pm    
thanks, nice work
Mohammad A Rahman 1-Aug-11 17:19pm    
Not a Problem :)
shaikh-adil 10-Dec-12 8:28am    
+5
Mohammad A Rahman 19-Dec-12 9:33am    
Thank you :)
shaikh-adil 19-Dec-12 9:59am    
you are welcome sir
What John means is this,

C#
this.Controls.Add(objtxtBox);
 
Share this answer
 
Comments
suniltikli 31-Jul-11 13:54pm    
yes i have used this but unable to find the control

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