c# - How to Dynamically create textboxes using ASP.NET and then save their values in Database? -
c# - How to Dynamically create textboxes using ASP.NET and then save their values in Database? -
i creating survey site. want add together textboxes dynamically , values in database.
now let's select 4 textboxes dropdown there dynamically.
code on selection on dropdown :
protected void numdropdown_selectedindexchanged(object sender, eventargs e) { if (dropdownlist1.selectedvalue == "textbox") { int j; = int.parse(numdropdown.selectedvalue); session["i"] = i; switch (i) { case 1: t = new textbox[i]; session["textbox"] = t; (j = 0; j < i; j++) { t[j] = new textbox(); t[j].id = "txtcheckbox" + j.tostring(); panel1.controls.add(t[j]); } break; case 2: t = new textbox[i]; session["textbox"] = t; (j = 0; j < i; j++) { t[j] = new textbox(); t[j].id = "txtcheckbox" + j.tostring(); panel1.controls.add(t[j]); } break; case 3: t = new textbox[i]; session["textbox"] = t; (j = 0; j < i; j++) { t[j] = new textbox(); t[j].id = "txtcheckbox" + j.tostring(); panel1.controls.add(t[j]); } break; case 4: t = new textbox[i]; list<textbox> mytextboxes; (j = 0; j < i; j++) { t[j] = new textbox(); t[j].id = "txtcheckbox" + j.tostring(); panel1.controls.add(t[j]); seek { mytextboxes = (list<textbox>)session["addedtextbox"]; mytextboxes.add(t[j]); session["addedtextbox"] = mytextboxes; } grab { mytextboxes = new list<textbox>(); mytextboxes.add(t[j]); session["addedtextbox"] = mytextboxes; } } break; } } }
2) here entered values in textbox a, b,c,d , click add:
code click on add together click :
1) first checked session there on page_init :
protected void page_init(object sender, eventargs e) { if (session["addedtextbox"] != null) { string a; string b; string c; string d; int listcount = ((list<textbox>)session["addedtextbox"]).count; foreach (textbox t in ((list<textbox>)session["addedtextbox"])) { if (listcount == 1) { } if (listcount == 2) { } if (listcount == 3) { } if (listcount == 4) { if (t.id == "txtcheckbox0") { = t.text; } if (t.id == "txtcheckbox0") { b = t.text; } if (t.id == "txtcheckbox0") { c = t.text; } if (t.id == "txtcheckbox0") { d = t.text; } } } }
but problem here don't text values, appear empty. please help me solve issue.
this sounds age-old classic problem asp.net adding controls dynamically page.
the problem viewstate , reconstruction of controls on postback.
you need run same code generated controls on postback at right time in page lifecycle ensure postback values match server-side controls.
of course of study if want hacky shortcut, access request.form["textcheckbox" + index]
values straight
a useful article on subject.
c# asp.net
Comments
Post a Comment