1

I am trying to populate textboxes (txtFirstName, txtSecondName) programatically by allowing the users to type in (say, 4) in a textbox and press the button to populate these into a panel. So if they put 2 in then they will get 2 rows of textboxes for first and last name. My problem is when I save I cannot get the text from these textboxes that were created on the fly. Any suggestions?

//button         
protected void Button1_Click(object sender, EventArgs e)
{
    int number = int.Parse(TextBox1.Text);

    for (int i = 0; i < number; i++)
    {
        //Horizontal line
        LiteralControl hrline = new LiteralControl();
        hrline.Text = "<hr />";

        //Textboxes
        TextBox txtFirstName = new TextBox();
        TextBox txtSecondName = new TextBox();

        //FirstName
        txtFirstName.ID = "txtFirstName" + i;
        txtFirstName.Text = "First Name " + i;

        //Second name
        txtSecondName.ID = "txtSecondName" + i;
        txtSecondName.Text = "Last Name " + i;

        buttonPanel.Controls.Add(hrline);
        pnlteacherExp.Controls.Add(txtFirstName);
        pnlteacherExp.Controls.Add(txtSecondName);
        pnlteacherExp.Controls.Add(hrline);
    }
}

Save button to save to database:

protected void btnSave_Click(object sender, EventArgs e)
{
    int number = int.Parse(TextBox1.Text);

    for (int i = 0; i < number; i++)
    {
        string connectionString = WebConfigurationManager.ConnectionStrings["crud_connection"].ConnectionString;
        SqlConnection sqlConnection1 = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand();

        cmd.CommandText = "Store_proc";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Connection = sqlConnection1;
        sqlConnection1.Open();

        cmd.Parameters.AddWithValue("@staffPayroll", "payroll_num");
        cmd.Parameters.AddWithValue("@FirstName", ??);
        cmd.Parameters.AddWithValue("@Surname", ??);

        cmd.ExecuteNonQuery();
        sqlConnection1.Close();
    }                
}
pixelmeow
  • 654
  • 1
  • 9
  • 31
wubblyjuggly
  • 919
  • 3
  • 13
  • 33
  • 1
    Dynamic controls are lost after postback. If you want to access values from the controls you created, you will need to create them latest in the page's `Load` event. For your case, you'd probably want to store the input value in session after the button has been clicked, then reload the page. Then, in the `Load` event, check for values stored in session and create the textboxes. – JW Lim Jun 26 '14 at 10:44
  • Or you could also reload the page with a query string instead of storing the input value in session. So you'd redirect to something like `~/MyPage.aspx?NoOfRowsToCreate=3`. – JW Lim Jun 26 '14 at 10:51

2 Answers2

2

You can get the post parametres using the Request.Form.

The posted parametres are use the name value of the rendered html element, or the UniqueID on server side, but because you can not set the UniqueID on server side, and because you make them dynamically, probably the name is rendered the same as the id, at least on my tests, and the line will be as:

cmd.Parameters.AddWithValue("@FirstName", 
       Request.Form["txtFirstName" + i.ToString()]);

To clarify, normally you need to do this Request.Form[control.UniqueID] to get the posted value, but because you do not have the control because you make it dynamically and is not there any more, the next is to get the post value with the posted name, so the posted name is this one "txtFirstName" + i.ToString() the way you makes them.

relative:
asp.net request.form
Accessing control client name and not ID in ASP.NET
Get info about Http Post field order

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
1

You can try the following:

   TextBox txtFirstName = Controls.FindControl(string.Format("txtFirstName{0}", i)) as TextBox;
   string name = txtFirstName.Text;

You can browse through this link to see how you can retain state of the dynamically created control: http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

Bayeni
  • 1,046
  • 9
  • 16