c# - Dynamically added TextBoxes empty on button click -


i'm adding textboxes dynamically , when click submit button , have postback, can't see values entered textboxes, coming emtpy. here's .aspx page...\

form id="form1" runat="server">         <asp:placeholder id="phformcontent" runat="server">          </asp:placeholder>         <br /><br />         <asp:button id="btnaddform" runat="server" text="add form" onclick="btnaddform_click" />         <asp:button id="btnsubmitforms" runat="server" text="submit forms" onclick="btnsubmit_click" />     </form> 

...here's how add textboxes form on clicking btnaddform...

protected void btnaddform_click(object sender, eventargs e)         {             // create labels             label lblname = new label();             lblname.text = "name:";             label lblnumber = new label();             lblnumber.text = "number:";             label lbladdress = new label();             lbladdress.text = "address:";             label lblcompany = new label();             lblcompany.text = "company:";              // create text boxes             textbox txtname = new textbox();             textbox txtnumber = new textbox();             textbox txtaddress = new textbox();             textbox txtcompany = new textbox();              // create submit button             button btnsubmit = new button();             btnsubmit.text = "submit";              // create panel , add controls             panel pnlform = new panel();             pnlform.controls.add(lblname);             pnlform.controls.add(txtname);             pnlform.controls.add(new literalcontrol("<br /><br />"));             pnlform.controls.add(lblnumber);             pnlform.controls.add(txtnumber);             pnlform.controls.add(new literalcontrol("<br /><br />"));             pnlform.controls.add(lbladdress);             pnlform.controls.add(txtaddress);             pnlform.controls.add(new literalcontrol("<br /><br />"));             pnlform.controls.add(lblcompany);             pnlform.controls.add(txtcompany);             pnlform.controls.add(new literalcontrol("<hr />"));             pnlform.controls.add(new literalcontrol("<br /><br />"));             panels.add(pnlform);              foreach (control panel in panels)             {                 phformcontent.controls.add(panel);             }         } 

...and here's how try extract fields each individual panel added...

private static void getformfields(control panelcontrol)      {         controlcollection controls = panelcontrol.controls;         foreach (control childcontrol in panelcontrol.controls)         {             if (childcontrol.gettype().tostring() == "system.web.ui.webcontrols.textbox")             {                 textbox txt = childcontrol textbox;                 fields.add(txt);             }             else             {                 getformfields(childcontrol);             }         }     } 

panels , fields static list, each panel containing 4 fields. pass getformfields individual panel reference...

private static list<control> panels = new list<control>();         private static list<textbox> fields = new list<textbox>(); 

try dynamically adding them on page_init event. typically ensure persist through postback. if can't this, you'll have @ preserving data manually storing in viewstate.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -