Loop all textbox and collect values in c# asp.net -


i'm trying list of strings database.

for each string in list want add label & textbox page.

on button submit want collect textbox value corresponding label value save database.

i need retrieving values textboxes.

what have far:

panel1 on aspx page

    protected list<string> items = myclass.getitems();       protected void page_load(object sender, eventargs e)     {         generateitemstable();     }      private void generateitemstable()     {         table table = new table();         table.id = "table1";         //placeholder1.controls.add(table);         panel1.controls.add(table);          foreach (var x in items)         {             tablerow row = new tablerow();             (int y = 0; y < 1; y++)             {                 tablecell labelcell = new tablecell();                 labelcell.controls.add(createlabel(x));                 labelcell.cssclass = "tdlabel";                 row.cells.add(labelcell);                  tablecell txbcell = new tablecell();                 txbcell.controls.add(createradnumerictextbox(x));                 txbcell.width = 30;                 row.cells.add(txbcell);                  tablecell datatypecell = new tablecell();                 datatypecell.text = "<span style='font-size: 10px; color: #777'>(student count)</span>";                 datatypecell.width = 100;                 row.cells.add(datatypecell);                  tablecell fourthcell = new tablecell();                 if (x == items[items.count - 1])                 {                     radbutton rb = new radbutton();                     rb.id = "submit";                     rb.text = "submit guidance";                     rb.skin = "forest";                     rb.click += new eventhandler(submit_click);                     rb.autopostback = true;                     fourthcell.controls.add(rb);                     row.cells.add(fourthcell);                 }                 else                 {                     row.cells.add(fourthcell);                 }              }             table.rows.add(row);         }     }      private radnumerictextbox createradnumerictextbox(string x)     {         radnumerictextbox rntb = new radnumerictextbox();         rntb.id = x;         rntb.width = 40;         return rntb;     }      private label createlabel(string x)     {         label l = new label();         l.id = "label_" + x;         l.text = "<label>" + x + "</label>";         return l;     }      protected void submit_click(object sender, eventargs e)     {         foreach (control x in findcontrol("panel1").findcontrol("table1").controls)         {             if (x radnumerictextbox)             {                 //how data??????/             }         }     } 

(thanks read whole post)

-----------------updated solution--------------------------------------------

i decided change , store list db @ page_load. list stored loop through list , use findcontrol() access textboxes. this..

//a couple containers protected class itemval {     public int value { get; set; }     public string name { get; set; } } protected list<itemval> items = new list<itemval>();    //get list database protected void getitems() {     foreach (var x in myclass.getitems())     {         itemval = new itemval();         i.name = x;         items.add(i);     } }  //submit protected void submit_click(object sender, eventargs e) {     foreach (var x in items)     {         radnumerictextbox rntb = findcontrol(x.name) radnumerictextbox;         x.value = (int)rntb.value;     } } 

you need cast x radnumerictextbox , pull out property values want, this:

radnumerictextbox theradnumerictextbox = x radnumerictextbox; string val = theradnumerictextbox.text; 

then other controls want, need put if conditions types, this:

if (x label) {     label thelabel = x label;     string vallabel = thelabel.text; } 

here full code method:

protected void submit_click(object sender, eventargs e) {     foreach (control x in findcontrol("panel1").findcontrol("table1").controls)     {         label thelabel;         radnumerictextbox theradnumerictextbox;          if (x radnumerictextbox)         {             radnumerictextbox theradnumerictextbox = x radnumerictextbox;             string val = theradnumerictextbox.text;         }          if (x label)         {             label thelabel = x label;             string vallabel = thelabel.text;         }          // either store in list or save database on each loop; recommended store list , send changes @ once database save, choice     } } 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -