user2516578
user2516578

Reputation: 19

How to get text from dynamically created textboxes in ASP.NET

I created dynamically some textboxes. They are created after click on one button(number of the textboxes depends on the user).

     protected void Button1_Click(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox2.Text);
        Table tbl = new Table();
        tbl.Width = Unit.Percentage(80);
        TableRow tr;
        TableCell tc;
        TextBox txt;
        CheckBox cbk;
        DropDownList ddl;
        Label lbl;
        Button btn;
        for (int j = 1; j <= i; j++)
        {
            tr = new TableRow();
            tc = new TableCell();
            tc.Width = Unit.Percentage(25);
            lbl = new Label();
            lbl.Text = "Pitanje:";
            tc.Controls.Add(lbl);
            tr.Cells.Add(tc);
            tc.Width = Unit.Percentage(25);
            txt = new TextBox();
            txt.ID = "txt_p_" + j;
            tc.Controls.Add(txt);
            tr.Cells.Add(tc);

            tc.Width = Unit.Percentage(25);
            lbl = new Label();
            lbl.Text = "Odgovori:";
            tc.Controls.Add(lbl);
            tr.Cells.Add(tc);
            tc.Width = Unit.Percentage(25);
            txt = new TextBox();
            txt.ID = "txt_o_" + j;
            tc.Controls.Add(txt);
            tr.Cells.Add(tc);
            tbl.Rows.Add(tr);

        }
        Panel1.Controls.Add(tbl);

    }

now I need to get the text that is typed into that textboxes. I tried with something that I found on the internet but can't get it to work.

     protected void SpremiPitanja(object sender, EventArgs e)
    {
        int i = Convert.ToInt32(TextBox2.Text);
        for (int j = 1; j <= i; j++)
        {
          ***************************************
        }
    }

any kind of help is welcome. if you need more information I will give them

Upvotes: 0

Views: 7490

Answers (2)

Iztoksson
Iztoksson

Reputation: 990

You add textboxes the same way you do, this is my example (sorry it's vb.net):

Dim t As New TextBox With {.ID = "txt_123", .Text = "Vpiši"}
PlaceHolder1.Controls.Add(t)
t = New TextBox With {.ID = "txt_456", .Text = "Briši"}
PlaceHolder1.Controls.Add(t)

And then you iterate through controls in Placeholder (in my example):

Dim tItem As TextBox
Dim tValue As String = String.Empty
For Each c As Control In PlaceHolder1.Controls
    If TypeOf c Is TextBox Then
        tItem = c
        tValue = tItem.Text.ToString
    End If
Next

C# example added

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        TextBox t = new TextBox();
        t.Text = "Vpiši";
        t.ID = "txt_123";
        PlaceHolder1.Controls.Add(t);

        t = new TextBox();
        t.Text = "Briši";
        t.ID = "txt_456";
        PlaceHolder1.Controls.Add(t);
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox tItem;
    String tValue;

    foreach (Control c in PlaceHolder1.Controls)
    {
        if (c.GetType() == typeof(TextBox))
        {
            tItem = (TextBox)c;
            tValue = tItem.Text;
        }
    }

}

Upvotes: 0

ChrisK
ChrisK

Reputation: 1218

A variable declared in a function is only visible in a function. You need to store the TextBoxes in a variable, that exists even when the code in the function has "finished". For more information search for scopes.

Here is a small sample that stores TextBoxes in a List that is visible in your class.

Another option would be to use eventhandlers. It depends on your scenario, which solution would be suited better. If you store the TextBoxes in a List, you can easily perform clean up code (for instance remove EventHandlers if required). You can obviously combine Approach 1 and 2. In that case you would store the created TextBox in a List (or any other collection), but you would still use the sender in the eventhandler to get a reference to the sending TextBox.

public partial class Form1 : Form
{
    List<TextBox> textBoxes = new List<TextBox>();

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Approach 1: create and add textbox to list
        TextBox createdTextbox = new TextBox();
        textBoxes.Add(createdTextbox);
    }

    private void button2_Click(object sender, EventArgs e)
    {
        //use the textboxes from the list
        foreach(TextBox t in textBoxes)
        {
            //do something with t
        }
    }

    private void button3_Click(object sender, EventArgs e)
    {
        //Approach 2: use eventhandlers and don't store textbox in a list
        TextBox createdTextbox = new TextBox();
        createdTextbox.TextChanged += createdTextbox_TextChanged;
        listBox1.Items.Add(createdTextbox);
    }

    void createdTextbox_TextChanged(object sender, EventArgs e)
    {
        TextBox t = sender as TextBox;

        if (t == null)
            throw new ArgumentException("sender not of type TextBox", "sender");

        //do something with t
    }
}

Upvotes: 1

Related Questions