c# - .NET retrieving values from dynamically created controls in a placeholder -


i have .net web form, c# backend.

i need looping through nested controls in placeholder. have single placeholder, creating dynamic form controls on fly based on data coming stored procedure. placeholder has single table created on fly. table populated checkboxes, , textboxes (they "attached" each other, id... not attached, have same numeric id component).

i need looping through placeholder, , table detect: checkboxes checked, , data in textboxes. not checkboxes have textboxes.

asp.net

<asp:placeholder id="phmystuff" runat="server"></asp:placeholder> 

c#

using (sqldatareader dr = cmd.executereader()) {     while (dr.read())     {         myflag= bool.parse(dr["myflag"].tostring());          checkbox cb = new checkbox();         cb.text = dr["stuffname"].tostring();         cb.id = dr["stuffid"].tostring();           tablerow tr = new tablerow();         tablecell td = new tablecell();         td.controls.add(cb);         tr.controls.add(td);          if (myflag== true)         {             textbox tb = new textbox();             tb.id = string.format("tb_{0}", dr["stuffid"].tostring());             tb.width = 100;             tablecell td2 = new tablecell();             td2.controls.add(tb);             tr.controls.add(td2);          }         mytable.controls.add(tr);     } } conn.close(); conn.dispose();  phmystuff.controls.add(mytable);  

the 2 pieces of data after in loop are:

  1. the id of checkboxes checked
  2. the value of textboxes created in placeholder

make sure code load data occurs on every postback using same id's each control each time , following:

protected void btngetcontrolids_click(object sender, eventargs e) {     getcontrolids(phmystuff); }  private void getcontrolids(control parent)  {     foreach (control ctrl in parent.controls)     {         if (ctrl textbox || ctrl checkbox)         {             response.write(ctrl.id);         }         else         {             if (ctrl.hascontrols())             {                 getcontrolids(ctrl);             }         }     } } 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -