0

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

2 Answers 2

1

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
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I created a list at start, added the textbox that is created in the list, and then in foreach tried the Label4.Text=t.Text; just to see does it work, but the text of label does not changes
That's odd... Can you post a reduced sample of your code that shows the problem?
in the question is the first part of code, where I create textboxes. There I just added textBoxes.Add(createdTextbox); Then later I just added foreach (TextBox t in textBoxes) { Label4.Text=t.Text; }
This way the Label4.Text will be the content of the last added TextBox. Is this what you want to do? Because for that you would not need to iterate over each TextBox.
can I add you to skype or something and then later just post the answer here?
0

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;
        }
    }

}

1 Comment

ChrisK is right in his answer, you will lose added controls with each postback unless you can manage to bring them back to the page. I forgot to mention that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.