Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
See more:
Hello to all!
I am making an ASP.net web application where I dynamically create a textbox.

C#
i=0;
public void dynamic()
{
    TextBox txt = new TextBox();
    txt.ID = i.ToString();
    txt.MaxLength = Convert.ToInt32(lenth);
    TableCell td2 = new TableCell();
    td2.Controls.Add(txt);
    tr.Cells.Add(td2);
    table.Rows.Add(tr);
    i++;
}
panel1.Controls.Add(table);

Now I also have a save button and I need to find the text of the dynamically created text box so I can save it in a database.
Please help me.

Thanks in advance!

[edit]Code block added to preserve formatting - OriginalGriff[/edit]
[edit]Code indentation, grammar and spelling, textspeak removed - Manfred R. Bihy[/edit]
Posted
Updated 7-Jul-16 22:05pm
v4

C#
foreach (Control c in panel1.Controls) {
  if (c is TextBox) {
    TextBox txt = (TextBox)c;
    string str = txt.Text;
  }
}



Or if you know the IDs of your textboxes.
TextBox txt = (TextBox)panel1.FindControl("txt");

Use txt.Text.
 
Share this answer
 
v2
Comments
Olivier Levrey 2-Mar-11 6:04am    
Good, my 5. (I edited just to fix the pre tags). But if there are more than one text box, the first loop might not be enough. Second solution seems more reliable.
Member 14369041 15-May-19 1:53am    
Why the below onchange call is not working
text.Attributes.Add("onchange", "text_change(text)");
It is working fine for JS file function.
I will add another possiblity.
When you create your textbox, you can attach a handler to the TextChanged event to keep an updated variable:

public void dynamic()
{
    TextBox txt = new TextBox();
    ...
    //add the event handler here
    txt.TextChanged += new EventHandler(System.EventHandler(this.txt_TextChanged));
    ...
}
//add a variable to keep the textbox content
string yourText = string.Empty;
//update the variable as soon as the content of the text box is changed
private void txt_TextChanged(object sender, EventArgs e)
{
    yourText = (sender as TextBox).Text;
}


Choose the solution you prefer ;)
 
Share this answer
 
Comments
Monjurul Habib 2-Mar-11 13:47pm    
That would be my answer too,my 5+.
Member 10398915 8-Apr-16 4:35am    
Useful! Thanks. :)
There are several ways to do this. First I would suggest to create your dynamic textboxes at InIt or Pre_Init functions only.
Now to retrieve the data, if you know the ids then you can directly access or you can find all the controls in your panel and find every textbox and get the value for each textbox at the time of saving
 
Share this answer
 
Comments
Olivier Levrey 2-Mar-11 6:06am    
Good. But providing some code could be even more helpful.
It is working code.

C#
protected void Page_Load(object sender, EventArgs e)
    {
	    TextBox Tbox = (TextBox)pnlGrid.FindControl("txtSearch");        
        Tbox.TextChanged += new EventHandler(txtSearch_TextChanged);      
	}
	
	protected void txtSearch_TextChanged(object sender, EventArgs e)
    {
        GridView grid = (GridView)pnlGrid.FindControl("AspGrid");
        TextBox Tbox = (TextBox)pnlGrid.FindControl("txtSearch");
        string searchText = Tbox.Text;
    }
 
Share this answer
 

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