Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get text from textbox created on backend

Status
Not open for further replies.
Feb 12, 2003
1
US
C#/asp.net

I have a TextBox that is created in a sub. It is created fine and with the correct id, but I'm unable to find it on submit. I get an error object reference not set to object. My code is below. Can someone please tell me what I'm doing wrong?

Code:
protected void MakeTB()
{
	System.Web.UI.WebControls.TextBox tb;
	tb = new System.Web.UI.WebControls.TextBox();
	tb.ID = "File1"
	TableCellFiles11.Controls.Add(tb);
}

protected void Submit(object sender, EventArgs e)
{
	TableCell tc = TableCellFiles11;
	tc.ID = TableCellFiles11.ID;
	System.Web.UI.WebControls.TextBox tb;
	tb = new System.Web.UI.WebControls.TextBox();
	tb = tc.FindControl("File1") as System.Web.UI.WebControls.TextBox;
	Response.Write(tb.Text);
}
 
Where are you calling MakeTB()?

This control has to be created on EACH Post Back, otherwise you will get the error you are getting.

Any why create the tb manually anyway? Creating controls and dynamically adding them to your page is a path that you really don't want to go down if you don't have to.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top