1

I'm trying to make a page that loads an undefined number of rows into a table, each row containing two columns, one column a string, the other a TextBox. When I click a submit button, i want to be able to retrieve the values entered into each TextBox.

The first half i can do, obviously in the real code this would be in a foreach loop and the textbox ID assigned a unique value i could reproduce later to call it with.

TableCell myCell = new TableCell();
myCell.Text = "StudentID";
TableCell nextCell = new TableCell();
TextBox mytext = new TextBox();
mytext.ID = "TxtBox1";
nextCell.Controls.Add(mytext);
TableRow myRow = new TableRow();
myRow.Cells.Add(myCell);
myRow.Cells.Add(nextCell);
TableStuUploads.Rows.Add(myRow);

When i click my submit button, i try to run this code(just temp code ATM, proof of concept stuff):

TextBox tmptext = (TextBox)FindControl("TxtBox1");
Label1.Text = tmptext.Text;

But that sets tmptext as null, and i get a null pointer exception on the next line. So then i tried

TextBox tmptext = (TextBox)TableStuUploads.FindControl("TxtBox1");
Label1.Text = tmptext.Text;

Same error. Then i tried

foreach (Control x in TableStuUploads.Controls)
            {
                if (x.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
                {
                    Label1.Text = ((TextBox)x).Text;

                }
            }

But when debugging this, I see TableStuUploads.Controls has a count of zero.

How am I supposed to address these dynamically created controls? I have searched around, and the answer i got lead me to the three solutions i have already tried. Where am i going wrong?

2 Answers 2

2

You are probably not recreating your dynamic controls on post back. This is required to both find each control and retrieve each control's value.

In addition, it would probably be best to use a repeater to create these controls, It is easier to maintain and debug.

Sign up to request clarification or add additional context in comments.

6 Comments

Ok, so your saying I need to run the function to create the table, on page load?
You can run that function during page load, although it is best to run it during the OnInit event.
Additionally, have you considered the Repeater suggestion I gave?
Thank you, recreating the table in the page load event worked. What are the advantages/differences with having the code run in the oninit event instead of Page_Load. And yes i looked into the Repeater suggestion, but really its more work than this page deserves, now i have this method working fine.
The end result is the same. Re-creating the controls during page load forces the page to play "catch up", while creating the controls during OnInit is the "correct" place during the page's lifecycle. Also, please don't forget to mark my answer as accepted if it solved your question. Thanks!
|
1

Edit

You need to recreate your table on each postback(page_load) like @ShaiCohen says in his answer then you need something ilke:

foreach (TableRow item in TableStuUploads.Rows)
{               
     TextBox tmptext = (TextBox)item.FindControl("TxtBox1");
     Label1.Text = tmptext.Text;
}

3 Comments

TableStuUploads.Rows also has a count of zero.
You need to recreate your table on each postback(page_load) like @ShaiCohen says in his answer, after that you can see the rows
Yes, after recreating the table in page_load, you code does now work, thank you very much :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.