How to dynamically create text fields using ASP.NET and then store their values ​​in a database?

I am creating a survey site. I want to add text fields dynamically and then get their values ​​in the database.

Now let's say that I select 4 text fields from the drop-down list to be there dynamically.

Code in the Select drop-down list:

protected void NumDropDown_SelectedIndexChanged(object sender, EventArgs e) { if (DropDownList1.SelectedValue == "TextBox") { int j; i = int.Parse(NumDropDown.SelectedValue); Session["i"] = i; switch (i) { case 1: t = new TextBox[i]; Session["textBox"] = t; for (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; for (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; for (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; for (j = 0; j < i; j++) { t[j] = new TextBox(); t[j].ID = "txtCheckbox" + j.ToString(); Panel1.Controls.Add(t[j]); try { MyTextBoxes = (List<TextBox>)Session["AddedTextBox"]; MyTextBoxes.Add(t[j]); Session["AddedTextBox"] = MyTextBoxes; } catch { MyTextBoxes = new List<TextBox>(); MyTextBoxes.Add(t[j]); Session["AddedTextBox"] = MyTextBoxes; } } break; } } } 

2) Then here I entered the values ​​in a text box like a, b, c, d and press ADD:

Code for click on ADD Click:

1) First, I checked the session so that it was on the page_Inte:

  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") { a = t.Text; } if (t.ID == "txtCheckbox0") { b = t.Text; } if (t.ID == "txtCheckbox0") { c = t.Text; } if (t.ID == "txtCheckbox0") { d = t.Text; } } } } 

But the problem here is that I am not getting text values, they seem empty. Please help me solve this problem.

+6
source share
2 answers

This sounds like an old classic problem when asp.net dynamically adds controls to a page.

The problem is presenting the state and restoring the controls during postback.

You need to run the same code that generated the controls for postback at the right time in the page life cycle to ensure that postback values ​​match the server-side controls.

Of course, if you need a hacker shortcut, directly go into the Request.Form["textCheckbox" + index] values

Useful article on this topic.

+3
source

As @ jenson-button-event is mentioned, you can access the values ​​of the TextBox via Request.Form here:

Aspx:

 <asp:DropDownList ID="ddlTextBoxes" runat="server"> <asp:ListItem Value="1" Text="1" /> <asp:ListItem Value="5" Text="5" /> </asp:DropDownList> <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" /><br /> <asp:PlaceHolder ID="container" runat="server" Visible="false"> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Submit" /> </asp:PlaceHolder> 

Code behind:

  protected void Add(object sender, EventArgs e) { int numOfTxt = Convert.ToInt32(ddlTextBoxes.SelectedItem.Value); var table = new Table(); for (int i = 0; i < numOfTxt; i++) { var row = new TableRow(); var cell = new TableCell(); TextBox textbox = new TextBox(); textbox.ID = "Textbox" + i; textbox.Width = new Unit(180); cell.Controls.Add(textbox); row.Cells.Add(cell); table.Rows.Add(row); } container.Controls.AddAt(0,table); container.Visible = true; } protected void Submit(object sender, EventArgs e) { var textboxValues = new List<string>(); if (Request.Form.HasKeys()) { Request.Form.AllKeys.Where(i => i.Contains("Textbox")).ToList().ForEach(i => { textboxValues.Add(Request.Form[i]); }); } //Do something with the textbox values textboxValues.ForEach(i => Response.Write(i + "<br />")); container.Visible = false; } 
+1
source

All Articles