Dynamicly Add Controls

G

gilly3

I need to be able to add form inputs to a form. A user can enter one item
and then click a link to add another.

So far I have a linkbutton the calls this function:

private void linkAddRow_Click(object sender, System.EventArgs e)
{
HtmlTableRow oNewRow = new HtmlTableRow();
HtmlTableCell oNewCell = new HtmlTableCell();
TextBox oNewTextBox = new TextBox();
oNewCell.Controls.Add(oNewTextBox);
oNewRow.Cells.Add(oNewCell);
oTable.Rows.Add(oNewRow);
}

It adds one row fine. But each additional time I click the link, I don't
get any more rows. Instead of generating a new row, it is only modifying
the existing object.

What approach should I use to dynamically add an unknown number of elements
to a page?

-ivan.
 
J

Jos

gilly3 said:
I need to be able to add form inputs to a form. A user can enter one
item and then click a link to add another.

So far I have a linkbutton the calls this function:

private void linkAddRow_Click(object sender, System.EventArgs e)
{
HtmlTableRow oNewRow = new HtmlTableRow();
HtmlTableCell oNewCell = new HtmlTableCell();
TextBox oNewTextBox = new TextBox();
oNewCell.Controls.Add(oNewTextBox);
oNewRow.Cells.Add(oNewCell);
oTable.Rows.Add(oNewRow);
}

It adds one row fine. But each additional time I click the link, I
don't get any more rows. Instead of generating a new row, it is only
modifying the existing object.

What approach should I use to dynamically add an unknown number of
elements to a page?

-ivan.

Dynamic controls don't get recreated automatically on postback.
Therefore, you should make sure they are recreated in your code.

I suggest adding a session or viewstate variable to keep track
of how many rows the user has, and using that variable
to recreate the table on postback.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top