c# - How to Programatically create Multiple textboxes in an aspx page and save to database? -



c# - How to Programatically create Multiple textboxes in an aspx page and save to database? -

i trying populate textboxes (txtfirstname, txtsecondname) programatically allowing users type in (say, 4) in textbox , press button populate these panel. if set 2 in 2 rows of textboxes first , lastly name. problem when save cannot text these textboxes created on fly. suggestions?

//button protected void button1_click(object sender, eventargs e) { int number = int.parse(textbox1.text); (int = 0; < number; i++) { //horizontal line literalcontrol hrline = new literalcontrol(); hrline.text = "<hr />"; //textboxes textbox txtfirstname = new textbox(); textbox txtsecondname = new textbox(); //firstname txtfirstname.id = "txtfirstname" + i; txtfirstname.text = "first name " + i; //second name txtsecondname.id = "txtsecondname" + i; txtsecondname.text = "last name " + i; buttonpanel.controls.add(hrline); pnlteacherexp.controls.add(txtfirstname); pnlteacherexp.controls.add(txtsecondname); pnlteacherexp.controls.add(hrline); } }

save button save database:

protected void btnsave_click(object sender, eventargs e) { int number = int.parse(textbox1.text); (int = 0; < number; i++) { string connectionstring = webconfigurationmanager.connectionstrings["crud_connection"].connectionstring; sqlconnection sqlconnection1 = new sqlconnection(connectionstring); sqlcommand cmd = new sqlcommand(); cmd.commandtext = "store_proc"; cmd.commandtype = commandtype.storedprocedure; cmd.connection = sqlconnection1; sqlconnection1.open(); cmd.parameters.addwithvalue("@staffpayroll", "payroll_num"); cmd.parameters.addwithvalue("@firstname", ??); cmd.parameters.addwithvalue("@surname", ??); cmd.executenonquery(); sqlconnection1.close(); } }

you can post parametres using request.form.

the posted parametres utilize name value of rendered html element, or uniqueid on server side, because can not set uniqueid on server side, , because create them dynamically, name rendered same id, @ to the lowest degree on tests, , line as:

cmd.parameters.addwithvalue("@firstname", request.form["txtfirstname" + i.tostring()]);

to clarify, need request.form[control.uniqueid] posted value, because not have control because create dynamically , not there more, next post value posted name, posted name 1 "txtfirstname" + i.tostring() way makes them.

relative: asp.net request.form accessing command client name , not id in asp.net get info http post field order

c# asp.net

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -