My goal is to... Click on a button and it creates some text boxes. User types in the info and if there is more info (in this case different houses they might be interested in ) to put in. They keep clicking the add button and more boxes appear. When they are done they click insert and all of the data inserts.
Right now what I have is a user can click it once and insert data and it works. What I am having trouble with is having a way to keep the ID counted in a way where I don't have to type in "txtDynamic1", "txtDyamic2", etc..
Also, the parameter @name can only be used once. So I don't understand if the only way to do this would be to click insert and then it wipes the data from the text box and they can retype for more.
I am not sure if my envision is possible where they could click however many adds for text boxes and then insert it all with one button click. I have a gridview for this set up but it does it one at a time as well... Any help would be so much appreciated as I have sunk way too many hours into this.
Sorry if this isn't explained clearly enough as my brain is fried at this point.
public partial class Sold : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
foreach (string key in keys)
{
this.CreateTextBox("txtDynamic" + i);
i++;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
int j = 1;
for (j = 1; j < 3; j++)
{
int index = pnl.Controls.OfType<TextBox>().ToList().Count + 1;
this.CreateTextBox("txtDynamic" + index);
}
}
private void CreateTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
pnl.Controls.Add(txt);
// Literal lt = new Literal();
// lt.Text = "<br />";
// pnl.Controls.Add(lt);
}
protected void Button2_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO TI_Homes(CustomerID, Year, Make, Size, Owed, Offer, Wholesale) VALUES('1000', @name, @year, '80ft', '100,000', '80,000', 'Wholesale?')"))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@name", (pnl.FindControl("txtDynamic1") as TextBox).Text);
cmd.Parameters.AddWithValue("@year", (pnl.FindControl("txtDynamic2") as TextBox).Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}