c# - Grab user input from dynamic text box -
i have 2 buttons
. 1 button creates textbox
, submits information. i'm having trouble retrieving users texts once textbox
has been created. here code:
private void createtextbox(int j) //creates fields / cells { textbox t = new textbox(); t.id = "textbox" + j; //t.text = "textbox" + j; lsttextbox.add(t); var c = new tablecell(); c.controls.add(t); r.cells.add(c); table1.rows.add(r); session["test"] = lsttextbox; } protected void button2_click(object sender, eventargs e) { string[] holder = new string[4]; (int = 0; < holder.length; i++) { holder[i] = ""; } list<textbox> lsttextbox = (session["test"] list<textbox>); if (lsttextbox.count < counter) { int = lsttextbox.count; (int j = 0; j < i; j++) { holder[j] = lsttextbox[j].text; } sqlconnection conns = new sqlconnection(configurationmanager.connectionstrings["testdbconnectionstring1"].connectionstring); sqlcommand cmd = new sqlcommand("insert loanerform (field0, field1, field2, field3) values (@field0, @field1, @field2, @field3)", conns); cmd.commandtype = commandtype.text; cmd.parameters.addwithvalue("@field0", holder[0]); cmd.parameters.addwithvalue("@field1", holder[1]); cmd.parameters.addwithvalue("@field2", holder[2]); cmd.parameters.addwithvalue("@field3", holder[3]); conns.open(); cmd.executenonquery(); conns.close(); } counter = 0; button1.visible = true; //going submit data sql }
thank in advance!
here how create textboxes dynamically. keeps track of number of textboxes in viewstate.
<asp:button runat="server" id="button1" onclick="button1_click" text="create textboxes" /> <asp:button runat="server" id="button2" onclick="button2_click" text="save textboxes database" /> <asp:placeholder runat="server" id="placeholder1"></asp:placeholder> public int counter { { return convert.toint32(viewstate["counter"] ?? "0"); } set { viewstate["counter"] = value; } } protected void page_load(object sender, eventargs e) { // need reload textboxes on page // otherwise, becomes null int total = counter; (int = 0; < total; i++) { var textbox = new textbox { id = "textbox" + i, text = "textbox" + }; placeholder1.controls.add(textbox); } } private void createtextbox(int id) { var textbox = new textbox { id = "textbox" + id, text = "textbox" + id }; placeholder1.controls.add(textbox); } protected void button1_click(object sender, eventargs e) { createtextbox(counter); counter = counter + 1; } protected void button2_click(object sender, eventargs e) { int total = counter; (int = 0; < total; i++) { var textbox = placeholder1.findcontrol("textbox" + i) textbox; var text = textbox.text; // text } }
Comments
Post a Comment