Safayat Zisan
Safayat Zisan

Reputation: 33

How to get values from dynamically created textboxes in asp.net

I have created some dynamic TextBox in ASP.NET Web Forms. Please somebody tell me, how I can get the text values of corresponding textboxes and save them to database. Here is my code:

for (int i = 0; i < n; i++)
{
     MyTextBox.ID = "tb" + "" + ViewState["num"] + i;
     this.PlaceHolder1.Controls.Add(MyTextBox);
}

Upvotes: 0

Views: 1069

Answers (1)

VDWWD
VDWWD

Reputation: 35514

You use FindControl:

    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < n; i++)
        {
            TextBox tb = FindControl("tb" + ViewState["num"] + i) as TextBox;
            string value = tb.Text;
        }
    }

Upvotes: 1

Related Questions