Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get values from dynamically added textboxes asp.net c#

as suggested in the title i have in which i can insert how many textboxes i want to add to a placeholder. i can add the textboxes just fine the problem is i cant get the values inserted on those dynamically added textboxes. here's my code

the purpose of this piece of code is to whenever the textbox in which i can introduce the number of textboxes i want. it creates and adds them to the placeholder in 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 submit and response.write the values inserted in all those textboxes.

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've been searching online and i've been getting answers that this code is fine and it should work but it doesnt. if you guys see anything wrong or have any suggestion that can solve my problem i'd really appreciate it

like image 655
aconstancio Avatar asked Mar 23 '14 13:03

aconstancio


2 Answers

You are almost there.

Problem

You need to reload those dynamically created textboxes on post back. Otherwise, they will become null, and you won't be able to find it.

In order to do that, you need to save those dynamically TextBoxes Ids in persistent 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 Behind

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;
        }
    }
}
like image 80
Win Avatar answered Oct 14 '22 05:10

Win


You are actually creating textboxes with property Text set to default = ""; So you need set 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 store your values into 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 is your values: enter image description here

EDIT: For super mega better understanding: Create one more textbox txtOutput then add button GetDataFromTextBoxesAndPutItBelow and create an event for that button `Click'. Event code:

    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: screen2

like image 41
Wallstrider Avatar answered Oct 14 '22 04:10

Wallstrider