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 GenerateTable(int rowsCount) { Table table = new Table(); table.ID = "Table1"; Page.Form.Controls.Add(table); const int colsCount = 4; //TableFooterRow footer = new TableFooterRow(); //Button btn = new Button(); //btn.Width = 120; //btn.Text = "Submit Records"; //btn.Click += new EventHandler(this.OnTestButtonClick); //cell.Controls.Add(btn); TableHeaderRow header = new TableHeaderRow(); TableHeaderCell headerTableCell1 = new TableHeaderCell(); headerTableCell1.Text = "Item Name"; header.Cells.Add(headerTableCell1); TableHeaderCell headerTableCell2 = new TableHeaderCell(); headerTableCell2.Text = "Unit"; header.Cells.Add(headerTableCell2); TableHeaderCell headerTableCell3 = new TableHeaderCell(); headerTableCell3.Text = "Quantity"; header.Cells.Add(headerTableCell3); TableHeaderCell headerTableCell4 = new TableHeaderCell(); headerTableCell4.Text = "Rate"; header.Cells.Add(headerTableCell4); TableHeaderCell headerTableCell5 = new TableHeaderCell(); headerTableCell5.Text = "Total"; header.Cells.Add(headerTableCell5); table.Rows.Add(header); connection.Open(); command = new SqlCommand(); command.CommandText = "dbo.sp_Get_Emp"; command.CommandType = CommandType.StoredProcedure; command.Connection = connection; command.Parameters.AddWithValue("@registrationId","1014"); da = new SqlDataAdapter(command); ds = new DataSet(); DataSet dsSecond = new DataSet(); da.Fill(ds, "FirstTab"); da.Fill(ds, "SecondTab"); for (int i = 0; i < rowsCount; i++) { TableRow row = new TableRow(); for (int k = colsCount-1;k < colsCount; k++) { TableCell cell = new TableCell(); DropDownList dr = new DropDownList(); dr.ID = "DropDownList_" + i + "Col_" +k; dr.DataSource = ds.Tables[0]; dr.DataTextField = "EmpName"; dr.DataValueField = "EmpId"; dr.DataBind(); cell.Controls.Add(dr); row.Cells.Add(cell); } for (int j = 0; j <= colsCount-1; j++) { TableCell cell = new TableCell(); TextBox tb = new TextBox(); tb.ID = "TextBoxRow_" + i + "Col_" + j; cell.Controls.Add(tb); row.Cells.Add(cell); } for (int l = colsCount+1; l < colsCount+2; l++) { UpdatePanel uptab = new UpdatePanel(); TableCell cell = new TableCell(); //cell.Controls.Add(this.BuildTestButton()); Button button = new Button(); button.Width = 120; button.Text = "Delete Row"; button.ID = "button" + i+"col"+l; button.Click += new EventHandler(this.OnTestButtonClick); uptab.ContentTemplateContainer.Controls.Add(button); cell.Controls.Add(button); row.Cells.Add(cell); } table.Rows.Add(row); //footer.Cells.Add(cell); } SetPreviousData(rowsCount, colsCount); rowsCount++; ViewState["RowsCount"] = rowsCount; }