Here I have created sample, in this I have added Rows and Columns to existing Table.
HTML
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add New Row" />
<table id="Table1" runat="server" border="0" cellpadding="0" cellspacing="0">
</table>
</div>
Code
private int numOfRows = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GenerateTable(numOfRows);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (ViewState["RowsCount"] != null)
{
numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString());
GenerateTable(numOfRows);
}
}
private void SetPreviousData(int rowsCount, int colsCount)
{
HtmlTable table = (HtmlTable)Page.FindControl("Table1");
if (table != null)
{
for (int i = 0; i < rowsCount; i++)
{
for (int j = 0; j < colsCount; j++)
{
TextBox tb = (TextBox)table.Rows[i].Cells[j].FindControl("TextBoxRow_" + i + "Col_" + j);
tb.Text = Request.Form["TextBoxRow_" + i + "Col_" + j];
}
}
}
}
private void GenerateTable(int rowsCount)
{
HtmlTable table = (HtmlTable)Page.FindControl("Table1");
const int colsCount = 3;
for (int i = 0; i < rowsCount; i++)
{
HtmlTableRow row = new HtmlTableRow();
for (int j = 0; j < colsCount; j++)
{
HtmlTableCell cell = new HtmlTableCell();
TextBox tb = new TextBox();
tb.ID = "TextBoxRow_" + i + "Col_" + j;
cell.Controls.Add(tb);
row.Cells.Add(cell);
}
table.Rows.Add(row);
}
SetPreviousData(rowsCount, colsCount);
rowsCount++;
ViewState["RowsCount"] = rowsCount;
}
Screenshot
