1

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();
            }
        }
    }
}
1
  • 1
    Just a suggestion but I would look into doing this client side with javascript. The reason being you are going to have trouble keeping state. When you click and add the textboxes and submit they will all disappear along with their values. Although what you're doing can be done you are going down the hardest path to get there. Commented Jul 24, 2015 at 17:25

1 Answer 1

1

Although I advise against trying to do this on the server side, what you need to do is keep their state on postback meaning you will have to store them in something like session or viewstate (boo) restore them on postback and then loop over the form controls in your button click. Then you will have access to the @name parameter line by line in the loop (only needing it once). Below is the idea but I haven't used web forms in years so you'll need to check the syntax.

protected void Button2_Click(object sender, EventArgs e)
{
     foreach(var control in Page.Controls)
   {
      if(control is TextBox)
      {

        if(((TextBox)control).ID.IndexOf("txtDynamic") != -1)
        {
        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", ((TextBox)control).Text);
                cmd.Parameters.AddWithValue("@year", (pnl.FindControl("txtDynamic2") as TextBox).Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }
          }
        }
      }
     }
   }
}
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.