I think you are confusing with Dynamic Controls... Incase you are creating each of the row in the Table Dynamically... You need to create it (with the same name) on every page cycle.. Whether it is postback or not...
In simple terms, if you are creating something that is not in the .aspx (Tag) page, in your code... Do it on every postback...
In your code, you are retrieving the Table from the Session, but you are not adding it again to the form... The below line does that.
this.form1.Controls.Add(TableId);
Here is a working code, for your question... hope this helps. Let me know, if you have any questions... Also you may try DataGrid with object Datasource... That will help you to retain the data on postback, without writing it to DB.
Table TableId = new Table();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TableId.ID = "MyTable";
}
else
{
TableId = (Table)Session["table"];
this.form1.Controls.Add(TableId);
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
Session["table"] = TableId;
}
protected void btnAddinRow_Click(object sender, EventArgs e)
{
int num_row = (TableId.Rows).Count;
TableRow r = new TableRow();
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
TextBox t = new TextBox();
t.ID = "textID" + num_row;
t.EnableViewState = true;
r.ID = "newRow" + num_row;
c1.ID = "newC1" + num_row;
c2.ID = "newC2" + num_row;
c1.Text = "New Cell - " + num_row;
c2.Controls.Add(t);
r.Cells.Add(c1);
r.Cells.Add(c2);
TableId.Rows.Add(r);
Session["table"] = TableId;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…