Get values ​​from dynamically added asp.net c # text fields

as suggested in heading i, in which I can insert how many text fields I want to add to the placeholder. I can add text fields, just the problem is that I cannot get the values ​​inserted into these dynamically added text fields. here is my code

The purpose of this piece of code is whenever there is a text field in which I can enter the number of text fields that I want. he creates and adds them to the placeholder on my page.

public void txtExtra_TextChanged(object sender, EventArgs e) { for (a = 1; a <= int.Parse(txtExtra.Text); a++) { TextBox txt = new TextBox(); txt.ID = "txtquestion" + a; pholder.Controls.Add(txt); } } 

This is the code of the button that will send and respond. Enter the values ​​inserted in all of these text fields.

 protected void btnConfirm_Click(object sender, EventArgs e) { foreach (Control ctr in pholder.Controls) { if (ctr is TextBox) { string value = ((TextBox)ctr).Text; Response.Write(value); } } } 

I searched online and I got answers that this code is ok and it should work, but it is not. if you guys see something wrong or have any suggestion that can solve my problem, I really appreciate that.

+6
source share
2 answers

You are almost there.

Problem

You need to reload these dynamically created text fields in response. Otherwise, they will become zero, and you will not be able to find them.

To do this, you need to keep these dynamic TextBoxes identifiers in a constant location, such as view state or session state.

Screen shot

enter image description here

Aspx

 Number of TextBoxes: <asp:TextBox runat="server" ID="CounterTextBox" OnTextChanged="CounterTextBox_TextChanged" AutoPostBack="True" /><br/> <asp:PlaceHolder runat="server" ID="TextBoxPlaceHolder" /><br/> <asp:Button runat="server" ID="ConfirmButton" Text="Confirm" OnClick="ConfirmButton_Click" /><br/> Result: <asp:Literal runat="server" ID="ResultLiteral"/> 

Code for

 private List<string> TextBoxIdCollection { get { var collection = ViewState["TextBoxIdCollection"] as List<string>; return collection ?? new List<string>(); } set { ViewState["TextBoxIdCollection"] = value; } } protected void Page_Load(object sender, EventArgs e) { foreach (string textboxId in TextBoxIdCollection) { var textbox = new TextBox {ID = textboxId}; TextBoxPlaceHolder.Controls.Add(textbox); } } protected void CounterTextBox_TextChanged(object sender, EventArgs e) { var collection = new List<string>(); int total; if (Int32.TryParse(CounterTextBox.Text, out total)) { for (int i = 1; i <= total; i++) { var textbox = new TextBox { ID = "QuestionTextBox" + i }; // Collect this textbox id collection.Add(textbox.ID); TextBoxPlaceHolder.Controls.Add(textbox); } TextBoxIdCollection= collection; } } protected void ConfirmButton_Click(object sender, EventArgs e) { foreach (Control ctr in TextBoxPlaceHolder.Controls) { if (ctr is TextBox) { string value = ((TextBox)ctr).Text; ResultLiteral.Text += value; } } } 
+4
source

In fact, you create text fields with the Text property set by default = ""; Therefore, you need to set the txt.Text property, for example:

  public void txtExtra_TextChanged(object sender, EventArgs e) { for (int a = 1; a <= int.Parse(txtExtra.Text); a++) { TextBox txt = new TextBox(); txt.ID = "txtquestion" + a; txt.Text = "Some text"; // Set some text here pholder.Controls.Add(txt); } } 

EDIT:

After that you can save your values ​​in the list:

 private static List<string> values = new List<string>(); protected void btnConfirm_Click(object sender, EventArgs e) { foreach (Control ctr in pholder.Controls) { if (ctr is TextBox) { string value = ((TextBox)ctr).Text; values.Add(value); // add values here } } } 

EDIT: Here are your values: enter image description here

EDIT: For a super mega better understanding: Create another txtOutput text box, then add the GetDataFromTextBoxesAndPutItBelow button and create an event for this `Click 'button. Event ID:

  protected void btnGetData_Click(object sender, EventArgs e) { for (int i = 0; i < values.Count; i++) txtOutput.Text += "Value from txtquestion1: " + values[i] + " "; } 

Screenshot looks like: screen2

+2
source

All Articles