How to get data from dynamically created text boxes?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    How to get data from dynamically created text boxes?

    I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have set text boxes and labels inside the table rows. I then added a button. All of these are done through code.

    The problem that i am having is i can get the value from a text box with resides inside the first panel (out side of panel that is dynamically created) but i am being unable to retrive date form text box that is inside tables which is inside dynamically created panel. Please advice.

    following code shows how the form is set up.
    Code:
        protected void LoadShipperControls()
        {
            TextBox m = new TextBox();
            m.ID = "txttest";
            spanel.Controls.Add(m);
    
            for (int i = 0; i < 1; i++)
            {
                //creating a pannel
                Panel newpanel = new Panel();
                newpanel.ID = "pickup" + i.ToString();
                newpanel.CssClass = "currentshipper";
    
                //creating table to display current shipper details
                #region Current Shipper
                //adding a table to the pannel
                Table tblShipper = new Table();
                tblShipper.Width = 500;
                tblShipper.CellPadding = 3;
                tblShipper.CellSpacing = 3;
                tblShipper.CssClass = "shippertable";
    
                //creating row 0
                #region Row 0
                //creating table row1
                TableRow tr0 = new TableRow();
    
                //creating table column 
                TableCell td = new TableCell();
                td.ColumnSpan = 2;
                td.CssClass = "subaa";
                Label td1header = new Label();
                td1header.ID = "lbltd1header" + i.ToString();
                td1header.Text = "Current Pick# " + i.ToString() + " Information";
                td.Controls.Add(td1header);
    
                //adding cells to the row
                tr0.Cells.Add(td);
    
                //adding row to the table
                tblShipper.Rows.Add(tr0);
                #endregion
    
                //creating table row 1
                #region Row 1
                TableRow tr1 = new TableRow();
    
                //creating table columns
                TableCell td11 = new TableCell();
                td11.CssClass = "cellheaderwidth";
                Label crecode = new Label();
                crecode.Text = "CRECode : ";
                crecode.ID = "lblcrecode" + i.ToString();
                td11.Controls.Add(crecode);
    
                TableCell td12 = new TableCell();
                td12.CssClass = "celldetail";
                TextBox tcrecode = new TextBox();
                tcrecode.Width = 55;
                tcrecode.ID = "txtcrecode" + i.ToString();
                td12.Controls.Add(tcrecode);
    
                //adding columns to the row
                tr1.Cells.Add(td11);
                tr1.Cells.Add(td12);
    
                //adding row to table
                tblShipper.Rows.Add(tr1);
    
                #endregion
    
                //creating row 2
                #region Row 2
                //creating table row 2
                TableRow tr2 = new TableRow();
    
                //creating table column
                TableCell td1 = new TableCell();
                td1.CssClass = "cellheaderwidth";
                Label cname = new Label();
                cname.ID = "lblcname" + i.ToString();
                cname.Text = "Company Name : ";
                td1.Controls.Add(cname);
    
                TableCell td2 = new TableCell();
                td2.CssClass = "celldetail";
                TextBox tcname = new TextBox();
                tcname.Width = 238;
                tcname.ID = "txtcname" + i.ToString();
                td2.Controls.Add(tcname);
    
                //adding cell to row2
                tr2.Cells.Add(td1);
                tr2.Cells.Add(td2);        
    
                //adding row to the table
                tblShipper.Rows.Add(tr2);
               
                #endregion
    
                //creating table row 3
                #region Row 3
                TableRow tr3 = new TableRow();
    
                //creating table column
                TableCell td31 = new TableCell();
                td31.CssClass = "cellheaderwidth";
                Label caddress = new Label();
    
    
                caddress.ID = "lblcaddress" + i.ToString();
                caddress.Text = "Address : ";
                td31.Controls.Add(caddress);
    
                TableCell td32 = new TableCell();
                td32.CssClass = "celldetail";
                TextBox tcaddress = new TextBox();
                tcaddress.Width = 300;
                tcaddress.ID = "txtcaddress" + i.ToString();            
                td32.Controls.Add(tcaddress);
    
                //adding columns to row
                tr3.Cells.Add(td31);
                tr3.Cells.Add(td32);
    
                //adding row to table;
                tblShipper.Rows.Add(tr3);
                #endregion
    
                //creating table row4
                #region Row 4
                TableRow tr4 = new TableRow();
    
                //creating table columns
                TableCell td41 = new TableCell();
                td41.CssClass = "cellheaderwidth";
                Label cphone = new Label();
                cphone.Text = "Phone # : ";
                cphone.ID = "lblcphone" + i.ToString();
                td41.Controls.Add(cphone);
    
                TableCell td42 = new TableCell();
                td42.CssClass = "celldetail";
                TextBox tcphone = new TextBox();
                tcphone.Width = 100;
                tcphone.ID = "txtcphone" + i.ToString();
                td42.Controls.Add(tcphone);
    
                //adding columns to the row
                tr4.Cells.Add(td41);
                tr4.Cells.Add(td42);
    
                //adding row to table
                tblShipper.Rows.Add(tr4);
                #endregion
    
                //creating table row 5
                #region Row 5
                TableRow tr5 = new TableRow();
    
                //creating table columns
                TableCell td51 = new TableCell();
                td51.CssClass = "cellheaderwidth";
                Label pickup = new Label();
                pickup.Text = "Pickup #: ";
                pickup.ID = "lblcpickup" + i.ToString();
                td51.Controls.Add(pickup);
    
                TableCell td52 = new TableCell();
                td52.CssClass = "celldetail";
                TextBox tpickup = new TextBox();
                tpickup.Width = 75;
                tpickup.ID = "txtpickup" + i.ToString();
                td52.Controls.Add(tpickup);
    
                //adding columns to the row
                tr5.Cells.Add(td51);
                tr5.Cells.Add(td52);
    
                //adding row to table
                tblShipper.Rows.Add(tr5);
                #endregion
    
                //creating table row 6
                #region Row 6
                TableRow tr6 = new TableRow();
    
                //creating table columns
                TableCell td61 = new TableCell();
                td61.CssClass = "cellheaderwidth";
                Label pudate = new Label();
                pudate.Text = "Pickup Date : ";
                pudate.ID = "lblcpudate" + i.ToString();
                td61.Controls.Add(pudate);
    
                TableCell td62 = new TableCell();
                td62.CssClass = "celldetail";
                TextBox tpudate = new TextBox();
                tpudate.Width = 75;
                tpudate.ID = "txtpudate" + i.ToString();
                td62.Controls.Add(tpudate);
    
                //adding columns to the row
                tr6.Cells.Add(td61);
                tr6.Cells.Add(td62);
    
                //adding row to table
                tblShipper.Rows.Add(tr6);
                #endregion
    
                //creating table row 7
                #region Row 7
                TableRow tr7 = new TableRow();
    
                //creating table columns
                TableCell td71 = new TableCell();
                td71.CssClass = "cellheaderwidth";
                Label putime = new Label();
                putime.Text = "Pickup Time : ";
                putime.ID = "lblcputime" + i.ToString();
                td71.Controls.Add(putime);
    
                TableCell td72 = new TableCell();
                td72.CssClass = "celldetail";
                TextBox tputime = new TextBox();
                tputime.Width = 75;
                tputime.ID = "txtputime" + i.ToString();
                td72.Controls.Add(tputime);
    
                //adding columns to the row
                tr7.Cells.Add(td71);
                tr7.Cells.Add(td72);
    
                //adding row to table
                tblShipper.Rows.Add(tr7);
                #endregion
    
                //adding table to the panel
                newpanel.Controls.Add(tblShipper);
                            
                #endregion
    
                
                //creating the edit form
                newpanel.Controls.Add(new LiteralControl(" <fieldset style='margin:5px;border:solid 2px #cdcdb4;text-align:left;width:900px'>"));
                newpanel.Controls.Add(new LiteralControl(" <legend style='color:#c00000' ><strong>Change Shipper # " +i.ToString() +"  Information </strong> </legend>"));
    
                //creating controls for edit
                #region Edit Controls
    
                Table EditShipper = new Table();
                EditShipper.Width = 875;
                EditShipper.CellSpacing = 3;
                EditShipper.CellPadding = 3;
                EditShipper.CssClass = "shippertable";
    
                //creating of Row 1
                #region Row 1
    
                TableRow etr1 = new TableRow();
    
                TableCell etd11 = new TableCell();
                etd11.CssClass = "shippercellchange1";
                Label ccode = new Label();
                ccode.ID = "lblccode" + i.ToString();
                ccode.Text = "Code : ";
                etd11.Controls.Add(ccode);
    
                TableCell etd12 = new TableCell();
                etd12.CssClass = "shippercellchange2";
                TextBox tccode = new TextBox();
                tccode.ID = "txtccode" + i.ToString();
                etd12.Controls.Add(tccode);
    
                TableCell etd13 = new TableCell();
                etd13.CssClass = "shippercellchange3";
                Label ecname = new Label();
                ecname.ID = "lblecname" + i.ToString();
                ecname.Text = "Company Name : ";            
                etd13.Controls.Add(ecname);
    
                TableCell etd14 = new TableCell();
                etd14.CssClass = "shippercellchange2";
                TextBox tecname = new TextBox();
                tecname.ID = "txtecname" + i.ToString();
                tecname.Width = 250;
                etd14.Controls.Add(tecname);
    
                TableCell etd15 = new TableCell();
                etd11.CssClass = "shippercellchange1";
                Button btnLookup = new Button();
                btnLookup.ID = "btnlookup" + i.ToString();
                btnLookup.Width = 120;
                btnLookup.Text = " Lookup ";
                btnLookup.BorderColor = System.Drawing.ColorTranslator.FromHtml("#c40000");
                btnLookup.BorderWidth = 1;
                btnLookup.BorderStyle = BorderStyle.Solid;
                btnLookup.Click += new EventHandler(btnLookup_Click);
    
                etd15.Controls.Add(btnLookup);
    
    
                //adding columns to the row
                etr1.Cells.Add(etd11);
                etr1.Cells.Add(etd12);
                etr1.Cells.Add(etd13);
                etr1.Cells.Add(etd14);
                etr1.Cells.Add(etd15);
    
                //adding row to the table
                EditShipper.Rows.Add(etr1);
    
                //adding table to the panel;
                newpanel.Controls.Add(EditShipper);
    
                #endregion
    
                //creating row 2
                #region Row 2
                TableRow etr2 = new TableRow();
    
                TableCell etd21 = new TableCell();
                etd21.ColumnSpan = 5;
                Label ex = new Label();
                ex.ID = "lblex" + i.ToString();
                ex.Text = "Select One: ";
                etd21.Controls.Add(ex);
                ListBox lstshipper = new ListBox();
                lstshipper.ID = "lstshipper" + i.ToString();
                lstshipper.Width = 800;
                lstshipper.Height = 75;
                etd21.Controls.Add(lstshipper);
    
                //adding column to row
                etr2.Cells.Add(etd21);
    
                //adding row to table
                EditShipper.Rows.Add(etr2);
    
                #endregion
    
                //creating row 3
                #region Row 3
                TableRow etr3 = new TableRow();
    
                TableCell etd31 = new TableCell();
                etd31.CssClass = "shippercellchange1";
                Label deldate = new Label();
                deldate.ID = "lbldeldate" + i.ToString();
                deldate.Text = "Delivery Date : ";
                etd31.Controls.Add(deldate);
    
                TableCell etd32 = new TableCell();
    
                etd32.CssClass = "shippercellchange2";
    
                TextBox tdeldate = new TextBox();
                tdeldate.ID = "txtdeldate" + i.ToString();
                etd32.Controls.Add(tdeldate);
    
                TableCell etd33 = new TableCell();
                etd33.CssClass = "shippercellchange3";
                Label deltime = new Label();
                deltime.ID = "lbldeltime" + i.ToString();
                deltime.Text = "Delivery Time : ";
                etd33.Controls.Add(deltime);
    
                TableCell etd34 = new TableCell();
                etd34.CssClass = "shippercellchange2";
                TextBox tdeltime = new TextBox();
                tdeltime.ID = "txtdeltime" + i.ToString();
                etd34.Controls.Add(tdeltime);
    
                //adding columns to the row;
                etr3.Cells.Add(etd31);
                etr3.Cells.Add(etd32);
                etr3.Cells.Add(etd33);
                etr3.Cells.Add(etd34);
    
                //adding row to the table
                EditShipper.Rows.Add(etr3);
    
                #endregion
    
                //creating row 4
                #region Row 4
                TableRow etr4 = new TableRow();
    
                TableCell etd41 = new TableCell();
                etd41.CssClass = "shippercellchange1";
                Label delnum = new Label();
                delnum.ID = "lbldelnum" + i.ToString();
                delnum.Text = "Delivery # : ";
                etd41.Controls.Add(delnum);
    
                TableCell etd42 = new TableCell();
                etd42.CssClass = "shippercellchange2";
                TextBox tdelnum = new TextBox();
                tdelnum.ID = "txtdelnum" + i.ToString();
                etd42.Controls.Add(tdelnum);
    
                TableCell etd43 = new TableCell();
                etd43.CssClass = "shippercellchange3";
                Label fcfs = new Label();
                fcfs.ID = "lblfcfs" + i.ToString();
                fcfs.Text = "FcFs: ";
                etd43.Controls.Add(fcfs);
    
                TableCell etd44 = new TableCell();
                etd44.CssClass = "shippercellchange2";
                CheckBox tfcfs = new CheckBox();
                tfcfs.ID = "txtfcfs" + i.ToString();
                etd44.Controls.Add(tfcfs);
    
                //adding columns to the row;
                etr4.Cells.Add(etd41);
                etr4.Cells.Add(etd42);
                etr4.Cells.Add(etd43);
                etr4.Cells.Add(etd44);
    
                //adding row to the table
                EditShipper.Rows.Add(etr4);
    
                #endregion
                #endregion
               
                newpanel.Controls.Add(new LiteralControl("</fieldset>"));
               
                //creating buttons
                #region Buttons for save and remove
                newpanel.Controls.Add(new LiteralControl("<div style='margin:10px;'>"));
    
                Button save = new Button();
                save.BorderStyle = BorderStyle.Solid;
                save.BorderColor = System.Drawing.ColorTranslator.FromHtml("#c40000");
                save.BorderWidth = 1;
                save.Width = 150;
                save.Text = "Save";
    
                Button Remove = new Button();
                Remove.BorderStyle = BorderStyle.Solid;
                Remove.BorderColor = System.Drawing.ColorTranslator.FromHtml("#c40000");
                Remove.BorderWidth = 1;
                Remove.Width = 150;
                Remove.Text = "Remove";
                //adding buttons
                newpanel.Controls.Add(save);
                newpanel.Controls.Add(Remove);
                newpanel.Controls.Add(new LiteralControl("</div>"));
                #endregion 
                
              
                //adding to the main panel
                spanel.Controls.Add(newpanel);
            }
    
    
        }
    Last edited by semomaniz; Sep 25 '08, 08:05 PM. Reason: misspelling
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    You should add id's to each table cell that you dynamically create so that you can access that control. Now you can easily use the FindControl method to find the control in each TableCell

    Code:
    TextBox tb = (TextBox)TableCell1.FindControl("txtNameofTextBox");
    if(tb != null)
    {
      tb.Text = "Something";
    }
    Nathan

    Comment

    Working...