Capture user input from a dynamic text field

I have two buttons . One button creates a Textbox and another that transmits information. I am having trouble finding user texts after creating a Textbox . Here is the code:

  private void CreateTextBox(int j) //Creates the 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]; for (int i = 0; i < holder.Length; i++) { holder[i] = ""; } List<TextBox> lstTextBox = (Session["test"] as List<TextBox>); if (lstTextBox.Count < Counter) { int i = lstTextBox.Count; for (int j = 0; j < i; j++) { holder[j] = lstTextBox[j].Text; } SqlConnection conns = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDBConnectionString1"].ConnectionString); SqlCommand cmd = new SqlCommand("Insert into 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 to submit data to SQL } 

Thank you in advance!

+4
source share
3 answers

This is how you dynamically create a TextBox. It keeps track of the number of text fields in the 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 to Database" /> <asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder> public int Counter { get { return Convert.ToInt32(ViewState["Counter"] ?? "0"); } set { ViewState["Counter"] = value; } } protected void Page_Load(object sender, EventArgs e) { // Need to reload those textboxes on page back // Otherwise, they will becomes null int total = Counter; for (int i = 0; i < total; i++) { var textBox = new TextBox { ID = "TextBox" + i, Text = "TextBox" + i }; 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; for (int i = 0; i < total; i++) { var textbox = PlaceHolder1.FindControl("TextBox" + i) as TextBox; var text = textbox.Text; // Do something with text } } 
+2
source

Do not store text fields in a session; rather, create them on the page.

The trick creates them at the right time EVERY time (i.e. with every PostBack). Try loading their OnLoad () for the page (or CreateChildControls (), if possible).

Once you do this, ASP.NET will automatically associate the input with the TextBox, and you can refer to them, as usual, or through the parent's .FindControl ().

0
source

I think you used a great program to generate dynamic text fields and insert into the database. To retrieve text from dynamically generated text fields, use the code below.

 Request.Form["Textbox" + i.ToString()] 

where "i" represents the number of text fields created.

For more information. Please check the link below.

how to insert value in sql db from asp.net dynamically generated text fields

0
source

Source: https://habr.com/ru/post/1496219/


All Articles