Click here to Skip to main content
15,887,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have a table table1 with 1 row containing 2 textbox and a radiobuttonlist in a ajax tabcontainer.
There is an add and remove button to add/remove new rows with 2textbox and a radiobuttonlist in the existing table1. And there is a submit button to show this values. But I could not get the value of these dynamically created textboxes. In a normal page(without using tabcontainer) it works fine.

Can anyone please help me...I tried 2days to find the solution. Please...

this is my code...

C#
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];
                  }
            }


Edit: Moved to code from answer (and formatted) - Ankur.
Posted
Updated 4-Nov-10 1:51am
v3
Comments
Sunasara Imdadhusen 4-Nov-10 3:03am    
Please provide snippet of code
LittleYellowBird 4-Nov-10 4:44am    
Hi, as already said you need to provide some code, Try to narrow down the problem to a smallish peice or at least show us how you are creating your dynamic textbox. :)
Ankur\m/ 4-Nov-10 7:53am    
Use 'Improve Question' below your answer if you need to edit/update your question. Adding more information as an answer doesn't make sense.

1 solution

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900