By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,759 Members | 1,778 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,759 IT Pros & Developers. It's quick & easy.

How to get data from dynamically created text boxes?

Expert 100+
P: 210
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.
Expand|Select|Wrap|Line Numbers
  1.  
  2.     protected void LoadShipperControls()
  3.     {
  4.         TextBox m = new TextBox();
  5.         m.ID = "txttest";
  6.         spanel.Controls.Add(m);
  7.  
  8.         for (int i = 0; i < 1; i++)
  9.         {
  10.             //creating a pannel
  11.             Panel newpanel = new Panel();
  12.             newpanel.ID = "pickup" + i.ToString();
  13.             newpanel.CssClass = "currentshipper";
  14.  
  15.             //creating table to display current shipper details
  16.             #region Current Shipper
  17.             //adding a table to the pannel
  18.             Table tblShipper = new Table();
  19.             tblShipper.Width = 500;
  20.             tblShipper.CellPadding = 3;
  21.             tblShipper.CellSpacing = 3;
  22.             tblShipper.CssClass = "shippertable";
  23.  
  24.             //creating row 0
  25.             #region Row 0
  26.             //creating table row1
  27.             TableRow tr0 = new TableRow();
  28.  
  29.             //creating table column 
  30.             TableCell td = new TableCell();
  31.             td.ColumnSpan = 2;
  32.             td.CssClass = "subaa";
  33.             Label td1header = new Label();
  34.             td1header.ID = "lbltd1header" + i.ToString();
  35.             td1header.Text = "Current Pick# " + i.ToString() + " Information";
  36.             td.Controls.Add(td1header);
  37.  
  38.             //adding cells to the row
  39.             tr0.Cells.Add(td);
  40.  
  41.             //adding row to the table
  42.             tblShipper.Rows.Add(tr0);
  43.             #endregion
  44.  
  45.             //creating table row 1
  46.             #region Row 1
  47.             TableRow tr1 = new TableRow();
  48.  
  49.             //creating table columns
  50.             TableCell td11 = new TableCell();
  51.             td11.CssClass = "cellheaderwidth";
  52.             Label crecode = new Label();
  53.             crecode.Text = "CRECode : ";
  54.             crecode.ID = "lblcrecode" + i.ToString();
  55.             td11.Controls.Add(crecode);
  56.  
  57.             TableCell td12 = new TableCell();
  58.             td12.CssClass = "celldetail";
  59.             TextBox tcrecode = new TextBox();
  60.             tcrecode.Width = 55;
  61.             tcrecode.ID = "txtcrecode" + i.ToString();
  62.             td12.Controls.Add(tcrecode);
  63.  
  64.             //adding columns to the row
  65.             tr1.Cells.Add(td11);
  66.             tr1.Cells.Add(td12);
  67.  
  68.             //adding row to table
  69.             tblShipper.Rows.Add(tr1);
  70.  
  71.             #endregion
  72.  
  73.             //creating row 2
  74.             #region Row 2
  75.             //creating table row 2
  76.             TableRow tr2 = new TableRow();
  77.  
  78.             //creating table column
  79.             TableCell td1 = new TableCell();
  80.             td1.CssClass = "cellheaderwidth";
  81.             Label cname = new Label();
  82.             cname.ID = "lblcname" + i.ToString();
  83.             cname.Text = "Company Name : ";
  84.             td1.Controls.Add(cname);
  85.  
  86.             TableCell td2 = new TableCell();
  87.             td2.CssClass = "celldetail";
  88.             TextBox tcname = new TextBox();
  89.             tcname.Width = 238;
  90.             tcname.ID = "txtcname" + i.ToString();
  91.             td2.Controls.Add(tcname);
  92.  
  93.             //adding cell to row2
  94.             tr2.Cells.Add(td1);
  95.             tr2.Cells.Add(td2);        
  96.  
  97.             //adding row to the table
  98.             tblShipper.Rows.Add(tr2);
  99.  
  100.             #endregion
  101.  
  102.             //creating table row 3
  103.             #region Row 3
  104.             TableRow tr3 = new TableRow();
  105.  
  106.             //creating table column
  107.             TableCell td31 = new TableCell();
  108.             td31.CssClass = "cellheaderwidth";
  109.             Label caddress = new Label();
  110.  
  111.  
  112.             caddress.ID = "lblcaddress" + i.ToString();
  113.             caddress.Text = "Address : ";
  114.             td31.Controls.Add(caddress);
  115.  
  116.             TableCell td32 = new TableCell();
  117.             td32.CssClass = "celldetail";
  118.             TextBox tcaddress = new TextBox();
  119.             tcaddress.Width = 300;
  120.             tcaddress.ID = "txtcaddress" + i.ToString();            
  121.             td32.Controls.Add(tcaddress);
  122.  
  123.             //adding columns to row
  124.             tr3.Cells.Add(td31);
  125.             tr3.Cells.Add(td32);
  126.  
  127.             //adding row to table;
  128.             tblShipper.Rows.Add(tr3);
  129.             #endregion
  130.  
  131.             //creating table row4
  132.             #region Row 4
  133.             TableRow tr4 = new TableRow();
  134.  
  135.             //creating table columns
  136.             TableCell td41 = new TableCell();
  137.             td41.CssClass = "cellheaderwidth";
  138.             Label cphone = new Label();
  139.             cphone.Text = "Phone # : ";
  140.             cphone.ID = "lblcphone" + i.ToString();
  141.             td41.Controls.Add(cphone);
  142.  
  143.             TableCell td42 = new TableCell();
  144.             td42.CssClass = "celldetail";
  145.             TextBox tcphone = new TextBox();
  146.             tcphone.Width = 100;
  147.             tcphone.ID = "txtcphone" + i.ToString();
  148.             td42.Controls.Add(tcphone);
  149.  
  150.             //adding columns to the row
  151.             tr4.Cells.Add(td41);
  152.             tr4.Cells.Add(td42);
  153.  
  154.             //adding row to table
  155.             tblShipper.Rows.Add(tr4);
  156.             #endregion
  157.  
  158.             //creating table row 5
  159.             #region Row 5
  160.             TableRow tr5 = new TableRow();
  161.  
  162.             //creating table columns
  163.             TableCell td51 = new TableCell();
  164.             td51.CssClass = "cellheaderwidth";
  165.             Label pickup = new Label();
  166.             pickup.Text = "Pickup #: ";
  167.             pickup.ID = "lblcpickup" + i.ToString();
  168.             td51.Controls.Add(pickup);
  169.  
  170.             TableCell td52 = new TableCell();
  171.             td52.CssClass = "celldetail";
  172.             TextBox tpickup = new TextBox();
  173.             tpickup.Width = 75;
  174.             tpickup.ID = "txtpickup" + i.ToString();
  175.             td52.Controls.Add(tpickup);
  176.  
  177.             //adding columns to the row
  178.             tr5.Cells.Add(td51);
  179.             tr5.Cells.Add(td52);
  180.  
  181.             //adding row to table
  182.             tblShipper.Rows.Add(tr5);
  183.             #endregion
  184.  
  185.             //creating table row 6
  186.             #region Row 6
  187.             TableRow tr6 = new TableRow();
  188.  
  189.             //creating table columns
  190.             TableCell td61 = new TableCell();
  191.             td61.CssClass = "cellheaderwidth";
  192.             Label pudate = new Label();
  193.             pudate.Text = "Pickup Date : ";
  194.             pudate.ID = "lblcpudate" + i.ToString();
  195.             td61.Controls.Add(pudate);
  196.  
  197.             TableCell td62 = new TableCell();
  198.             td62.CssClass = "celldetail";
  199.             TextBox tpudate = new TextBox();
  200.             tpudate.Width = 75;
  201.             tpudate.ID = "txtpudate" + i.ToString();
  202.             td62.Controls.Add(tpudate);
  203.  
  204.             //adding columns to the row
  205.             tr6.Cells.Add(td61);
  206.             tr6.Cells.Add(td62);
  207.  
  208.             //adding row to table
  209.             tblShipper.Rows.Add(tr6);
  210.             #endregion
  211.  
  212.             //creating table row 7
  213.             #region Row 7
  214.             TableRow tr7 = new TableRow();
  215.  
  216.             //creating table columns
  217.             TableCell td71 = new TableCell();
  218.             td71.CssClass = "cellheaderwidth";
  219.             Label putime = new Label();
  220.             putime.Text = "Pickup Time : ";
  221.             putime.ID = "lblcputime" + i.ToString();
  222.             td71.Controls.Add(putime);
  223.  
  224.             TableCell td72 = new TableCell();
  225.             td72.CssClass = "celldetail";
  226.             TextBox tputime = new TextBox();
  227.             tputime.Width = 75;
  228.             tputime.ID = "txtputime" + i.ToString();
  229.             td72.Controls.Add(tputime);
  230.  
  231.             //adding columns to the row
  232.             tr7.Cells.Add(td71);
  233.             tr7.Cells.Add(td72);
  234.  
  235.             //adding row to table
  236.             tblShipper.Rows.Add(tr7);
  237.             #endregion
  238.  
  239.             //adding table to the panel
  240.             newpanel.Controls.Add(tblShipper);
  241.  
  242.             #endregion
  243.  
  244.  
  245.             //creating the edit form
  246.             newpanel.Controls.Add(new LiteralControl(" <fieldset style='margin:5px;border:solid 2px #cdcdb4;text-align:left;width:900px'>"));
  247.             newpanel.Controls.Add(new LiteralControl(" <legend style='color:#c00000' ><strong>Change Shipper # " +i.ToString() +"  Information </strong> </legend>"));
  248.  
  249.             //creating controls for edit
  250.             #region Edit Controls
  251.  
  252.             Table EditShipper = new Table();
  253.             EditShipper.Width = 875;
  254.             EditShipper.CellSpacing = 3;
  255.             EditShipper.CellPadding = 3;
  256.             EditShipper.CssClass = "shippertable";
  257.  
  258.             //creating of Row 1
  259.             #region Row 1
  260.  
  261.             TableRow etr1 = new TableRow();
  262.  
  263.             TableCell etd11 = new TableCell();
  264.             etd11.CssClass = "shippercellchange1";
  265.             Label ccode = new Label();
  266.             ccode.ID = "lblccode" + i.ToString();
  267.             ccode.Text = "Code : ";
  268.             etd11.Controls.Add(ccode);
  269.  
  270.             TableCell etd12 = new TableCell();
  271.             etd12.CssClass = "shippercellchange2";
  272.             TextBox tccode = new TextBox();
  273.             tccode.ID = "txtccode" + i.ToString();
  274.             etd12.Controls.Add(tccode);
  275.  
  276.             TableCell etd13 = new TableCell();
  277.             etd13.CssClass = "shippercellchange3";
  278.             Label ecname = new Label();
  279.             ecname.ID = "lblecname" + i.ToString();
  280.             ecname.Text = "Company Name : ";            
  281.             etd13.Controls.Add(ecname);
  282.  
  283.             TableCell etd14 = new TableCell();
  284.             etd14.CssClass = "shippercellchange2";
  285.             TextBox tecname = new TextBox();
  286.             tecname.ID = "txtecname" + i.ToString();
  287.             tecname.Width = 250;
  288.             etd14.Controls.Add(tecname);
  289.  
  290.             TableCell etd15 = new TableCell();
  291.             etd11.CssClass = "shippercellchange1";
  292.             Button btnLookup = new Button();
  293.             btnLookup.ID = "btnlookup" + i.ToString();
  294.             btnLookup.Width = 120;
  295.             btnLookup.Text = " Lookup ";
  296.             btnLookup.BorderColor = System.Drawing.ColorTranslator.FromHtml("#c40000");
  297.             btnLookup.BorderWidth = 1;
  298.             btnLookup.BorderStyle = BorderStyle.Solid;
  299.             btnLookup.Click += new EventHandler(btnLookup_Click);
  300.  
  301.             etd15.Controls.Add(btnLookup);
  302.  
  303.  
  304.             //adding columns to the row
  305.             etr1.Cells.Add(etd11);
  306.             etr1.Cells.Add(etd12);
  307.             etr1.Cells.Add(etd13);
  308.             etr1.Cells.Add(etd14);
  309.             etr1.Cells.Add(etd15);
  310.  
  311.             //adding row to the table
  312.             EditShipper.Rows.Add(etr1);
  313.  
  314.             //adding table to the panel;
  315.             newpanel.Controls.Add(EditShipper);
  316.  
  317.             #endregion
  318.  
  319.             //creating row 2
  320.             #region Row 2
  321.             TableRow etr2 = new TableRow();
  322.  
  323.             TableCell etd21 = new TableCell();
  324.             etd21.ColumnSpan = 5;
  325.             Label ex = new Label();
  326.             ex.ID = "lblex" + i.ToString();
  327.             ex.Text = "Select One: ";
  328.             etd21.Controls.Add(ex);
  329.             ListBox lstshipper = new ListBox();
  330.             lstshipper.ID = "lstshipper" + i.ToString();
  331.             lstshipper.Width = 800;
  332.             lstshipper.Height = 75;
  333.             etd21.Controls.Add(lstshipper);
  334.  
  335.             //adding column to row
  336.             etr2.Cells.Add(etd21);
  337.  
  338.             //adding row to table
  339.             EditShipper.Rows.Add(etr2);
  340.  
  341.             #endregion
  342.  
  343.             //creating row 3
  344.             #region Row 3
  345.             TableRow etr3 = new TableRow();
  346.  
  347.             TableCell etd31 = new TableCell();
  348.             etd31.CssClass = "shippercellchange1";
  349.             Label deldate = new Label();
  350.             deldate.ID = "lbldeldate" + i.ToString();
  351.             deldate.Text = "Delivery Date : ";
  352.             etd31.Controls.Add(deldate);
  353.  
  354.             TableCell etd32 = new TableCell();
  355.  
  356.             etd32.CssClass = "shippercellchange2";
  357.  
  358.             TextBox tdeldate = new TextBox();
  359.             tdeldate.ID = "txtdeldate" + i.ToString();
  360.             etd32.Controls.Add(tdeldate);
  361.  
  362.             TableCell etd33 = new TableCell();
  363.             etd33.CssClass = "shippercellchange3";
  364.             Label deltime = new Label();
  365.             deltime.ID = "lbldeltime" + i.ToString();
  366.             deltime.Text = "Delivery Time : ";
  367.             etd33.Controls.Add(deltime);
  368.  
  369.             TableCell etd34 = new TableCell();
  370.             etd34.CssClass = "shippercellchange2";
  371.             TextBox tdeltime = new TextBox();
  372.             tdeltime.ID = "txtdeltime" + i.ToString();
  373.             etd34.Controls.Add(tdeltime);
  374.  
  375.             //adding columns to the row;
  376.             etr3.Cells.Add(etd31);
  377.             etr3.Cells.Add(etd32);
  378.             etr3.Cells.Add(etd33);
  379.             etr3.Cells.Add(etd34);
  380.  
  381.             //adding row to the table
  382.             EditShipper.Rows.Add(etr3);
  383.  
  384.             #endregion
  385.  
  386.             //creating row 4
  387.             #region Row 4
  388.             TableRow etr4 = new TableRow();
  389.  
  390.             TableCell etd41 = new TableCell();
  391.             etd41.CssClass = "shippercellchange1";
  392.             Label delnum = new Label();
  393.             delnum.ID = "lbldelnum" + i.ToString();
  394.             delnum.Text = "Delivery # : ";
  395.             etd41.Controls.Add(delnum);
  396.  
  397.             TableCell etd42 = new TableCell();
  398.             etd42.CssClass = "shippercellchange2";
  399.             TextBox tdelnum = new TextBox();
  400.             tdelnum.ID = "txtdelnum" + i.ToString();
  401.             etd42.Controls.Add(tdelnum);
  402.  
  403.             TableCell etd43 = new TableCell();
  404.             etd43.CssClass = "shippercellchange3";
  405.             Label fcfs = new Label();
  406.             fcfs.ID = "lblfcfs" + i.ToString();
  407.             fcfs.Text = "FcFs: ";
  408.             etd43.Controls.Add(fcfs);
  409.  
  410.             TableCell etd44 = new TableCell();
  411.             etd44.CssClass = "shippercellchange2";
  412.             CheckBox tfcfs = new CheckBox();
  413.             tfcfs.ID = "txtfcfs" + i.ToString();
  414.             etd44.Controls.Add(tfcfs);
  415.  
  416.             //adding columns to the row;
  417.             etr4.Cells.Add(etd41);
  418.             etr4.Cells.Add(etd42);
  419.             etr4.Cells.Add(etd43);
  420.             etr4.Cells.Add(etd44);
  421.  
  422.             //adding row to the table
  423.             EditShipper.Rows.Add(etr4);
  424.  
  425.             #endregion
  426.             #endregion
  427.  
  428.             newpanel.Controls.Add(new LiteralControl("</fieldset>"));
  429.  
  430.             //creating buttons
  431.             #region Buttons for save and remove
  432.             newpanel.Controls.Add(new LiteralControl("<div style='margin:10px;'>"));
  433.  
  434.             Button save = new Button();
  435.             save.BorderStyle = BorderStyle.Solid;
  436.             save.BorderColor = System.Drawing.ColorTranslator.FromHtml("#c40000");
  437.             save.BorderWidth = 1;
  438.             save.Width = 150;
  439.             save.Text = "Save";
  440.  
  441.             Button Remove = new Button();
  442.             Remove.BorderStyle = BorderStyle.Solid;
  443.             Remove.BorderColor = System.Drawing.ColorTranslator.FromHtml("#c40000");
  444.             Remove.BorderWidth = 1;
  445.             Remove.Width = 150;
  446.             Remove.Text = "Remove";
  447.             //adding buttons
  448.             newpanel.Controls.Add(save);
  449.             newpanel.Controls.Add(Remove);
  450.             newpanel.Controls.Add(new LiteralControl("</div>"));
  451.             #endregion 
  452.  
  453.  
  454.             //adding to the main panel
  455.             spanel.Controls.Add(newpanel);
  456.         }
  457.  
  458.  
  459.     }
  460.  
  461.  
Sep 25 '08 #1
Share this Question
Share on Google+
1 Reply


nateraaaa
Expert 100+
P: 663
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

Expand|Select|Wrap|Line Numbers
  1. TextBox tb = (TextBox)TableCell1.FindControl("txtNameofTextBox");
  2. if(tb != null)
  3. {
  4.   tb.Text = "Something";
  5. }
Nathan
Sep 25 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.