Hi,
I was hoping someone could lend me a hand. I have a button and want to add one row to a table every time I click that button. The thing is, it works find the first time, but the second, third, ... the row is simply overwritten. Does anyone know what I'm doing wrong? I simply want to append a new row to the end of the table. This is the C# code:
1 protected void Button1_Click(object sender, EventArgs e) 2 { 3 TableRow row = new TableRow(); 4 5 for (int i = 0; i < 5; ++i) 6 { 7 TableCell cell = new TableCell(); 8 cell.Controls.Add(new LiteralControl(Guid.NewGuid().ToString())); 9 row.Cells.Add(cell); 10 } 11 12 Table1.Rows.Add(row); 13 } 14Thanks in advance.
![]() |
0 |
![]() |
hi,
that's coz the page is stateless, the first time you add the new row, it works ok, then, the page post back, and after it,
the page forget all about the new row added to the table, so when you add a new row the second time,
it only displayed one row not two as you expected.
one method is store the table in the session object
protected void b1_Click(object sender, EventArgs e)
{
if (Session["table"] == null)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = "abc";
tr.Cells.Add(tc);
Table1.Rows.Add(tr);
Session["table"] = Table1;
}
else
{
Table t= (Table)Session["table"];
TableRow tr = new TableRow();
TableCell tc = new TableCell();
tc.Text = "abc";
tr.Cells.Add(tc);
t.Rows.Add(tr);
form1.Controls.Add(t);
Session["table"] = t;
}
}
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
![]() |
0 |
![]() |
try enabling the viewstate of the table like the code below. i'm not sure it will work but i believe that the use of the property enableviewstate<table enableviewstate="true" > </table>
![]() |
0 |
![]() |
hi,
It cannot be implemented by just adding eableViewState="true"
thx
jessica
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
![]() |
0 |
![]() |
aaah, thank for the correction. then what does enableviewstate do? i thought it will behave just like other server control if you set to true the enableviewstate.
![]() |
0 |
![]() |
the enableview state help the control to retain the value populated at runtime. for instance, if you populated your DDL in the
if(!IsPostBack)
{ populate your DDL
}
then each time the page postback, the DDL doesn't lose the value.That's coz the EnableViewState=true help the DDL retain the value populate when the page first load.
but you add the table row at runtime, the row is a part of the table control, not data stored in the table,so the EnableViewState doesn't take effect in this situation.
hope it helps
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
![]() |
0 |
![]() |
yes it really help. I have another question. what do you mean by the data stored in the table? is it the text inside the table?
![]() |
0 |
![]() |
Thanks Jessica. Now it works fine
![]() |
0 |
![]() |
hi ulysses098,
I just mean like this you add the table cell's text in the first time the page load
if (!IsPostBack)
{
Table1.Rows[0].Cells[1].Text = "abc";
}and then each time the page postbacks, the table will retain the text value
if you set enableViewState= false, then the table will lose the text after postback
hope it helps
jessica
Jessica Cao
Sincerely,
Microsoft Online Community Support
“Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. ”
![]() |
0 |
![]() |
Hi jessica,
Thanks to the responds. I have clearly understand now. thank you very much on answering my question.ulysses098
![]() |
0 |
![]() |