H
Harlan Messinger
[sorry for the multipost but I got no response in m.p.d.f.a.webcontrols]
If I have a dropdown list ddl, and the following code appears in
Page_Load, then on each postback the rows previously added persist and I
wind up with more and more copies of the same rows. To avoid this, I
have to wrap the lines in an if (!Page.IsPostBack) block.
ddl.Items.Add("One");
ddl.Items.Add("Two");
ddl.Items.Add("Three");
But if I have a table control Table1 and do the same thing, the table
starts from scratch upon postback, so only the rows currently being
added appear in the table. Therefore, this code doesn't go in an if
(!Page.IsPostBack) block.
string[] keys = Request.Headers.AllKeys;
foreach (string key in keys)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Text = key;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = Request.Headers[key];
row.Cells.Add(cell);
Table1.Rows.Add(row);
}
Both the dropdown list control and the table control have
EnableViewState=true. So why the difference in behavior? For any given
control, how do I know which behavior to expect?
If I have a dropdown list ddl, and the following code appears in
Page_Load, then on each postback the rows previously added persist and I
wind up with more and more copies of the same rows. To avoid this, I
have to wrap the lines in an if (!Page.IsPostBack) block.
ddl.Items.Add("One");
ddl.Items.Add("Two");
ddl.Items.Add("Three");
But if I have a table control Table1 and do the same thing, the table
starts from scratch upon postback, so only the rows currently being
added appear in the table. Therefore, this code doesn't go in an if
(!Page.IsPostBack) block.
string[] keys = Request.Headers.AllKeys;
foreach (string key in keys)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Text = key;
row.Cells.Add(cell);
cell = new TableCell();
cell.Text = Request.Headers[key];
row.Cells.Add(cell);
Table1.Rows.Add(row);
}
Both the dropdown list control and the table control have
EnableViewState=true. So why the difference in behavior? For any given
control, how do I know which behavior to expect?