Reputation: 969
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();
}
}
Upvotes: 1
Views: 1195
Reputation: 1046
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
Upvotes: 1
Reputation: 66641
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
Upvotes: 2