you have to remember to recreate all the rows you created, consider to use DataGrid and a Session variable, for example:<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<script language="C#" runat="server"> DataTable GetDataTable()
{
DataTable tbl;
if (Session["MyData"] == null)
{
tbl = new DataTable();
tbl.Columns.Add("col1", typeof(int));
tbl.Columns.Add("col2",typeof(string));
Session["MyData"] = tbl;
}
return (DataTable)Session["MyData"];
} void BindData()
{
DataGrid1.DataSource = GetDataTable().DefaultView;
DataGrid1.DataBind();
} void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
void AddRow(Object Sender, EventArgs e)
{
DataTable dt = GetDataTable();
DataRow dr = dt.NewRow();
dr[0] = dt.Rows.Count + 1;
dr[1] = new Random().Next().ToString();
dt.Rows.Add(dr);
BindData();
}
</script>
<form runat=server>
<asp:DataGrid id="DataGrid1" runat="server" GridLines="Both"/>
<asp:Button id=btn1 runat=server Text=Add Onclick="AddRow" />
<asp:Button id=btn2 runat=server Text=Submit />
</form>
<%@ Import Namespace="System.Data" %>
<script language="C#" runat="server"> DataTable GetDataTable()
{
DataTable tbl;
if (Session["MyData"] == null)
{
tbl = new DataTable();
tbl.Columns.Add("col1", typeof(int));
tbl.Columns.Add("col2",typeof(string));
Session["MyData"] = tbl;
}
return (DataTable)Session["MyData"];
} void BindData()
{
DataGrid1.DataSource = GetDataTable().DefaultView;
DataGrid1.DataBind();
} void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
void AddRow(Object Sender, EventArgs e)
{
DataTable dt = GetDataTable();
DataRow dr = dt.NewRow();
dr[0] = dt.Rows.Count + 1;
dr[1] = new Random().Next().ToString();
dt.Rows.Add(dr);
BindData();
}
</script>
<form runat=server>
<asp:DataGrid id="DataGrid1" runat="server" GridLines="Both"/>
<asp:Button id=btn1 runat=server Text=Add Onclick="AddRow" />
<asp:Button id=btn2 runat=server Text=Submit />
</form>
{
-->if ( !IsPostBack )
{
TableRow tmpRow;
TableCell tmpCell;
for (int i=0;i<=2;i++)
{
tmpRow=new TableRow();
tmpRow.BackColor=Color.White;
tmpRow.Height=18;
tmpRow.HorizontalAlign=HorizontalAlign.Center;
tmpCell=new TableCell();
tmpCell.Text="A"+i.ToString();
tmpRow.Cells.Add(tmpCell); tmpCell=new TableCell();
tmpCell.Text="B"+i.ToString();
tmpRow.Cells.Add(tmpCell); tmpCell=new TableCell();
tmpCell.Text="C"+i.ToString();
tmpRow.Cells.Add(tmpCell);
myTab.Rows.Add(tmpRow);
}
}
}