Problems in getting values from dynamically added controls in C#
<p>I have to dynamically create controls and add them to a table. In a button click I need to find the value entered for that control. The control ids are also dynamic. Below is the code I tried.</p>

<pre><code>private void createUdfControls()
{
        colUdfNames.Clear();//this is a collection object.
        //the from id for gl is 3.
        colUdfNames = BL_GeneralLedger.GetAllUdfNames(3);
        if (colUdfNames.Count&gt;0)
        {
            TableRow tr1 = null; ;

            for(int i=0;i&lt;colUdfNames.Count;i++)
            {
               TableRow tr;
                if(i%2==0)
                {
                     tr = new TableRow();
                     tr1 = tr;
                }

                string udfTypeValue = colUdfNames[i].UdfTypeValue;
                int EnumUdfTypeId = colUdfNames[i].Enum_UdfTypeId;
                TableCell cellUdf1 = new TableCell();
                TableCell cellUdfValue1 = new TableCell();
                TableCell cellUdf2 = new TableCell();
                TableCell cellUdfValue2 = new TableCell();

                switch (EnumUdfTypeId)
                {

                    case 1:
                        {
                            //Text

                            if (i % 2 == 0)
                            {
                                cellUdf1.Text = colUdfNames[i].UdfName;
                                TextBox txtText = new TextBox();
                                txtText.ID = "txtText" + colUdfNames[i].UdfId;
                                txtText.MaxLength = colUdfNames[i].Width;
                                txtText.Text = "ww";
                                cellUdfValue1.Controls.Add(txtText);



                            }
                            else
                            {
                                cellUdf2.Text = colUdfNames[i].UdfName;
                                TextBox txtText = new TextBox();
                                txtText.ID = "txtText" + colUdfNames[i].UdfId;
                                txtText.MaxLength = colUdfNames[i].Width;
                                txtText.Text = "ww";
                                cellUdfValue2.Controls.Add(txtText);

                            }


                            break;
                        }
                    case 2:
                        {
                            //Number

                            if(i%2==0)
                            {
                                cellUdf1.Text = colUdfNames[i].UdfName;
                                TextBox txtNumber = new TextBox();
                                txtNumber.ID = "txtNumber" + colUdfNames[i].UdfId;
                                txtNumber.MaxLength = colUdfNames[i].Width;
                                txtNumber.Text = "12";
                                cellUdfValue1.Controls.Add(txtNumber);
                                DataRow dr = dtUdfControlInfo.NewRow();
                                dr[0] = txtNumber.ID;
                                dtUdfControlInfo.Rows.Add(dr);

                            }
                            else
                            {
                                cellUdf2.Text = colUdfNames[i].UdfName;
                                TextBox txtNumber = new TextBox();
                                txtNumber.ID = "txtNumber" + colUdfNames[i].UdfId;
                                txtNumber.MaxLength = colUdfNames[i].Width;
                                txtNumber.Text = "12";
                                cellUdfValue2.Controls.Add(txtNumber);
                                DataRow dr = dtUdfControlInfo.NewRow();
                                dr[0] = txtNumber.ID;
                                dtUdfControlInfo.Rows.Add(dr);

                            }


                            break;
                        }
                    case 3:
                        {
                            //Decimal

                            if(i%2==0)
                            {
                                cellUdf1.Text = colUdfNames[i].UdfName;
                                TextBox txtDecimal = new TextBox();
                                txtDecimal.ID = "txtDecimal" + colUdfNames[i].UdfId;
                                txtDecimal.MaxLength = colUdfNames[i].Width;
                                txtDecimal.Text = "2.2";
                                cellUdfValue1.Controls.Add(txtDecimal);


                            }
                            else
                            {
                                cellUdf2.Text = colUdfNames[i].UdfName;
                                TextBox txtDecimal = new TextBox();
                                txtDecimal.ID = "txtDecimal" + colUdfNames[i].UdfId;
                                txtDecimal.MaxLength = colUdfNames[i].Width;
                                txtDecimal.Text = "2.2";
                                cellUdfValue2.Controls.Add(txtDecimal);


                            }


                            break;
                        }
                    case 4:
                        {
                            //Memo

                            if(i%2==0)
                            {
                                cellUdf1.Text = colUdfNames[i].UdfName;
                                TextBox txtMemo = new TextBox();
                                txtMemo.TextMode = TextBoxMode.MultiLine;
                                txtMemo.ID = "txtMemo" + colUdfNames[i].UdfId;
                                txtMemo.MaxLength = colUdfNames[i].Width;
                                txtMemo.Text = "memo";
                                cellUdfValue1.Controls.Add(txtMemo);
                                DataRow dr = dtUdfControlInfo.NewRow();
                                dr[0] = txtMemo.ID;
                                dtUdfControlInfo.Rows.Add(dr);



                            }
                            else
                            {
                                cellUdf2.Text = colUdfNames[i].UdfName;
                                TextBox txtMemo = new TextBox();
                                txtMemo.TextMode = TextBoxMode.MultiLine;
                                txtMemo.ID = "txtMemo" + colUdfNames[i].UdfId;
                                txtMemo.MaxLength = colUdfNames[i].Width;
                                txtMemo.Text = "memo";
                                cellUdfValue2.Controls.Add(txtMemo);
                                DataRow dr = dtUdfControlInfo.NewRow();
                                dr[0] = txtMemo.ID;
                                dtUdfControlInfo.Rows.Add(dr);


                            }

                            break;
                        }
                    case 5:
                        {
                            //date

                            if(i%2==0)
                            {

                                cellUdf1.Text = colUdfNames[i].UdfName;
                                TextBox txtDate = new TextBox();
                                txtDate.ID = "txtDate" + colUdfNames[i].UdfId;
                                txtDate.MaxLength = colUdfNames[i].Width;
                                txtDate.Text = "12.07.2010";
                                cellUdfValue1.Controls.Add(txtDate);

                            }
                            else
                            {
                                cellUdf2.Text = colUdfNames[i].UdfName;
                                TextBox txtDate = new TextBox();
                                txtDate.ID = "txtDate" + colUdfNames[i].UdfId;
                                txtDate.MaxLength = colUdfNames[i].Width;
                                txtDate.Text = "12.07.2010";
                                cellUdfValue2.Controls.Add(txtDate);

                            }

                            break;
                        }
                    case 6:
                        {
                            //Datetime
                            if(i%2==0)
                            {
                                cellUdf1.Text = colUdfNames[i].UdfName;
                                TextBox txtDateTime = new TextBox();
                                txtDateTime.ID = "txtDateTime" + colUdfNames[i].UdfId;
                                txtDateTime.MaxLength = colUdfNames[i].Width;
                                txtDateTime.Text = "12.07.2010";
                                cellUdfValue1.Controls.Add(txtDateTime);

                            }
                            else
                            {
                                cellUdf2.Text = colUdfNames[i].UdfName;
                                TextBox txtDateTime = new TextBox();
                                txtDateTime.ID = "txtDateTime" + colUdfNames[i].UdfId;
                                txtDateTime.MaxLength = colUdfNames[i].Width;
                                txtDateTime.Text = "12.07.2010";
                                cellUdfValue2.Controls.Add(txtDateTime);

                            }


                            break;
                        }
                    case 7:
                        {
                            //"Dropdown"

                            if(i%2==0)
                            {
                                cellUdf1.Text = colUdfNames[i].UdfName;
                                DropDownList ddlDropDown = new DropDownList();
                                ddlDropDown.ID = "ddlDropDown" + colUdfNames[i].UdfId;
                                cellUdfValue1.Controls.Add(ddlDropDown);                                    
                                LoadUdfList(ddlDropDown, colUdfNames[i].UdfId);

                                if (colUdfNames[i].IsMandatory)
                                {
                                    ddlDropDown.Items.Insert(0, new ListItem("Select " + colUdfNames[i].UdfName, "0", true));
                                }


                            }
                            else
                            {
                                cellUdf2.Text = colUdfNames[i].UdfName;
                                DropDownList ddlDropDown = new DropDownList();
                                ddlDropDown.ID = "ddlDropDown" + colUdfNames[i].UdfId;
                                cellUdfValue2.Controls.Add(ddlDropDown);                                    
                                LoadUdfList(ddlDropDown, colUdfNames[i].UdfId);
                                if (colUdfNames[i].IsMandatory)
                                {
                                    ddlDropDown.Items.Insert(0, new ListItem("Select " + colUdfNames[i].UdfName, "0", true));
                                }
                            }

                            break;
                        }
                    case 8:
                        {
                            //"Checkbox"
                            if(i%2==0)
                            {
                                cellUdf1.Text = colUdfNames[i].UdfName;
                                CheckBox ChkBox = new CheckBox();
                                ChkBox.ID = "ChkBox" + colUdfNames[i].UdfId;
                                ChkBox.Checked = true;
                                cellUdfValue1.Controls.Add(ChkBox);  

                            }
                            else
                            {
                                cellUdf2.Text = colUdfNames[i].UdfName;
                                CheckBox ChkBox = new CheckBox();
                                ChkBox.ID = "ChkBox" + colUdfNames[i].UdfId;
                                ChkBox.Checked = true;
                                cellUdfValue2.Controls.Add(ChkBox);

                            }

                            break;
                        }
                }

                if (i % 2 == 0)
                {
                    tr1.Cells.AddAt(0, cellUdf1);
                    tr1.Cells.AddAt(1, cellUdfValue1);
                }
                else
                {
                    tr1.Cells.AddAt(2, cellUdf2);
                    tr1.Cells.AddAt(3, cellUdfValue2);
                }                                   

                tblUdf.Rows.Add(tr1);
               // tblUdf.DataBind();
            }


        }

    }
</code></pre>

<p>and   the  read function as below</p>

<p>private void readValuesFromUdfControls()
        {
            colUdfDataMaster.Clear();
           int c= tblUdf.Controls.Count;
           int tablrow= tblUdf.Rows.Count;</p>

<pre><code>       //TextBox tb1 = (TextBox)tblUdf.Rows[0].FindControl(dtUdfControlInfo.Rows[0][0].ToString());
       //string tb1 = (string)Request.Form["ctl00_ContentPlaceHolder1_" + dtUdfControlInfo.Rows[0][0].ToString()];

       for (int j = 0; j &lt; tblUdf.Rows.Count;j++ )
       {
           for (int k = 0; k &lt; tblUdf.Rows[j].Cells.Count;k++ )
           {
               Control ctrl = tblUdf.Rows[j].Cells[k].Controls[1];

               for (int i = 0; i &lt; colUdfNames.Count; i++)
               {
                   PL_UdfDataMaster objUdfDataMaster = new PL_UdfDataMaster();
                   if (ctrl.ID.Contains("txtText"))
                   {
                       TextBox tb = (TextBox)ctrl;
                       objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
                       objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
                       objUdfDataMaster.Enum_FormId = 3;
                       objUdfDataMaster.UdfData = tb.Text;
                   }
                   else if (ctrl.ID.Contains("txtNumber"))
                   {
                       TextBox tb = (TextBox)ctrl;
                       objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
                       objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
                       objUdfDataMaster.Enum_FormId = 3;
                       objUdfDataMaster.UdfData = tb.Text;
                   }
                   else if (ctrl.ID.Contains("txtDecimal"))
                   {
                       TextBox tb = (TextBox)ctrl;
                       objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
                       objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
                       objUdfDataMaster.Enum_FormId = 3;
                       objUdfDataMaster.UdfData = tb.Text;
                   }
                   else if (ctrl.ID.Contains("txtMemo"))
                   {
                       TextBox tb = (TextBox)ctrl;
                       objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
                       objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
                       objUdfDataMaster.Enum_FormId = 3;
                       objUdfDataMaster.UdfData = tb.Text;
                   }
                   else if (ctrl.ID.Contains("txtDate"))
                   {
                       TextBox tb = (TextBox)ctrl;
                       objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
                       objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
                       objUdfDataMaster.Enum_FormId = 3;
                       objUdfDataMaster.UdfData = tb.Text;
                   }
                   else if (ctrl.ID.Contains("txtDateTime"))
                   {
                       TextBox tb = (TextBox)ctrl;
                       objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
                       objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
                       objUdfDataMaster.Enum_FormId = 3;
                       objUdfDataMaster.UdfData = tb.Text;
                   }
                   else if (ctrl.ID.Contains("ddlDropDown"))
                   {
                       DropDownList ddl = (DropDownList)ctrl;
                       objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
                       objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
                       objUdfDataMaster.Enum_FormId = 3;
                       objUdfDataMaster.UdfData = ddl.SelectedItem.Text;
                   }
                   else if (ctrl.ID.Contains("ChkBox"))
                   {
                       CheckBox chk = (CheckBox)ctrl;
                       objUdfDataMaster.UdfId = colUdfNames[i].UdfId;
                       objUdfDataMaster.MasterId = ErpSessions._GeneralLedgerId;
                       objUdfDataMaster.Enum_FormId = 3;
                       objUdfDataMaster.UdfData = chk.Checked == true ? "true" : "false";
                   }
                   colUdfDataMaster.Add(objUdfDataMaster);
               }

           }



       }
    }
</code></pre>

<p>the problem is that i m getting the rowcount=0 of the table tblUdf inside which i added the table rows and controls inside the table cells.</p>

<p>Any help how to get the control values of the dynamically added controls.</p>

<p>Thankx.
Mohak</p>
