0

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);
}
2
  • Use CssClass property of the asp:textbox and get by classname and postback using javascript. Values in dynamic textbox refreshes with every page load. Commented Aug 14, 2016 at 17:12
  • That is not the way to get the values in code behind using ASP.NET. Commented Aug 14, 2016 at 20:42

1 Answer 1

1

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

Comments

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.