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();
}
}
Loadevent. 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 theLoadevent, check for values stored in session and create the textboxes.~/MyPage.aspx?NoOfRowsToCreate=3.