table1
private int numOfRows = 1; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GenerateTable(numOfRows); } } protected void btnAdd_Click(object sender, EventArgs e) { if (ViewState["RowsCount"] != null) { numOfRows = Convert.ToInt32(ViewState["RowsCount"].ToString()); GenerateTable(numOfRows); } } private void GenerateTable(int rowsCount) { int colsCount = 4; for (int i = 1; i < rowsCount; i++) { TableRow row = new TableRow(); TableCell cell1 = new TableCell(); Label label = new Label(); label.ID = "medicalDepLblNo" + i; label.Text = (i + 1).ToString(); cell1.Controls.Add(label); row.Cells.Add(cell1); TableCell cell2 = new TableCell(); TextBox textBox = new TextBox(); textBox.ID = "medicalDepTxtName" + i; textBox.Width = 204; cell2.Controls.Add(textBox); row.Cells.Add(cell2); TableCell cell3 = new TableCell(); textBox = new TextBox(); textBox.ID = "medicalDepTxtAge" + i; textBox.Width = 60; cell3.Controls.Add(textBox); row.Cells.Add(cell3); TableCell cell4 = new TableCell(); RadioButtonList rBtnList = new RadioButtonList(); rBtnList.ID = "medicalDepRbtnListSex" + i; rBtnList.RepeatDirection = RepeatDirection.Horizontal; rBtnList.Items.Add("Male"); rBtnList.Items.Add("Female"); rBtnList.SelectedIndex = 0; cell4.Controls.Add(rBtnList); row.Cells.Add(cell4); // And finally, add the TableRow to the Table TableDependant.Rows.Add(row); } //Set Previous Data on PostBacks SetPreviousData(rowsCount, colsCount); //Sore the current Rows Count in ViewState rowsCount++; ViewState["RowsCount"] = rowsCount; } private void SetPreviousData(int rowsCount, int colsCount) { for (int i = 1; i < rowsCount; i++) { //Extracting the Dynamic Controls from the Table Label lbl1 = (Label)TableDependant.Rows[i].Cells[0].FindControl("medicalDepLblNo" + i); lbl1.Text = Request.Form["medicalDepLblNo" + i]; TextBox txtBox1 = (TextBox)TableDependant.Rows[i].Cells[1].FindControl("medicalDepTxtName" + i); txtBox1.Text = Request.Form["medicalDepTxtName" + i]; TextBox txtBox2 = (TextBox)TableDependant.Rows[i].Cells[2].FindControl("medicalDepTxtAge" + i); txtBox2.Text = Request.Form["medicalDepTxtAge" + i]; RadioButtonList rbtnList1 = (RadioButtonList)TableDependant.Rows[i].Cells[3].FindControl("medicalDepRbtnListSex" + i); rbtnList1.SelectedValue = Request.Form["medicalDepRbtnListSex" + i]; } } protected void btnSubmitMedical_Click(object sender, EventArgs e) { int num = int.Parse(medicalDdlNoofDependants.SelectedValue); string[] names = new string[10]; for (int i = 0; i < num; i++) { names[i] = Request.Form["medicalDepTxtName" + i]; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)