Adding a Row containing Controls to a Table at Runtime. (ASP.NET / C#)

R

Richard Wilson

Hi

I'm trying to add a row containing 9 columns to an existing Table on a
web page.
Each new column contains a Text Box Control.
This I've placed in a Button Click Event

When I run the page. The event works fine for the first row. Then on
subsequent clicks all that seems to happen is the 1st new row that was
added is added again (so I lose any data that was in the Text Boxes.

Please help. I think I must be missing something obvious.

Many thanks in advance.

Richard Wilson
RVC

---------------------------------

private void Button1_Click(object sender, System.EventArgs e)
{

// Generate rows and cells.

int numcells = 9;
TableRow r = new TableRow();
for (int i=0; i<numcells; i++)
{
TableCell c = new TableCell();
TextBox textb = new TextBox();
textb.ID = "txtR" + Table1.Rows.Count + "C" + i;
switch(i)
{
case 0:
textb.Width = 30;
break;
case 1:
textb.Width = 50;
break;
case 2:
textb.Width = 40;
break;
case 3:
textb.Width = 40;
break;
case 4:
textb.Width = 40;
break;
case 5:
textb.Width = 50;
break;
case 6:
textb.Width = 80;
break;
case 7:
textb.Width = 80;
break;
case 8:
textb.Width = 80;
break;
}

textb.EnableViewState = true;
c.Controls.Add(textb);
r.Cells.Add(c);
}
Table1.Rows.Add(r);


}
 

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

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,743
Latest member
WoodrowMea

Latest Threads

Top