Grab user input from dynamic text box

2019-06-28 04:28发布

问题:

I have two buttons. One button that creates the Textbox and another that submits the information. I'm having trouble retrieving the users texts once the textbox has been created. Here is the code:

 private void CreateTextBox(int j) //Creates the fields / cells
    {


            TextBox t = new TextBox();
            t.ID = "Textbox" + j;
            //t.Text = "Textbox" + j;
            lstTextBox.Add(t);
            var c = new TableCell();
            c.Controls.Add(t);
            r.Cells.Add(c);
            table1.Rows.Add(r);
            Session["test"] = lstTextBox;

    }
protected void Button2_Click(object sender, EventArgs e)
    {
        string[] holder = new string[4];
        for (int i = 0; i < holder.Length; i++)
        {
            holder[i] = "";
        }
        List<TextBox> lstTextBox = (Session["test"] as List<TextBox>);
        if (lstTextBox.Count < Counter)
        {
            int i = lstTextBox.Count;
            for (int j = 0; j < i; j++)
            {

                holder[j] = lstTextBox[j].Text;

            }
            SqlConnection conns = new SqlConnection(ConfigurationManager.ConnectionStrings["TestDBConnectionString1"].ConnectionString);
            SqlCommand cmd = new SqlCommand("Insert into LoanerForm (field0, field1, field2, field3) Values (@field0, @field1, @field2, @field3)", conns);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@field0", holder[0]);
            cmd.Parameters.AddWithValue("@field1", holder[1]);
            cmd.Parameters.AddWithValue("@field2", holder[2]);
            cmd.Parameters.AddWithValue("@field3", holder[3]);
            conns.Open();
            cmd.ExecuteNonQuery();
            conns.Close();

        }

        Counter = 0;

        Button1.Visible = true; //Going to submit data to SQL

    }

Thank you in advance!

回答1:

Here is how you create TextBoxes dynamically. It keeps track of the number of textboxes in ViewState.

<asp:Button runat="server" ID="Button1" OnClick="Button1_Click" 
 Text="Create TextBoxes" />
<asp:Button runat="server" ID="Button2" OnClick="Button2_Click"      
  Text="Save TextBoxes to Database" />
<asp:PlaceHolder runat="server" ID="PlaceHolder1"></asp:PlaceHolder>

public int Counter
{
    get { return Convert.ToInt32(ViewState["Counter"] ?? "0"); }
    set { ViewState["Counter"] = value; }
}

protected void Page_Load(object sender, EventArgs e)
{
    // Need to reload those textboxes on page back
    // Otherwise, they will becomes null
    int total = Counter;
    for (int i = 0; i < total; i++)
    {
        var textBox = new TextBox
        {
            ID = "TextBox" + i,
            Text = "TextBox" + i
        };
        PlaceHolder1.Controls.Add(textBox);
    }
}

private void CreateTextBox(int id)
{
    var textBox = new TextBox
    {
        ID = "TextBox" + id,
        Text = "TextBox" + id
    };
    PlaceHolder1.Controls.Add(textBox);
}

protected void Button1_Click(object sender, EventArgs e)
{
    CreateTextBox(Counter);
    Counter = Counter + 1;
}

protected void Button2_Click(object sender, EventArgs e)
{
    int total = Counter;
    for (int i = 0; i < total; i++)
    {
        var textbox = PlaceHolder1.FindControl("TextBox" + i) as TextBox;
        var text = textbox.Text;
        // Do something with text
    }
}


回答2:

Don't store the TextBoxes in Session; rather, create them on the page.

The trick is creating them at the right time EVERY time (i.e. with each PostBack). Try loading them OnLoad() for the page (or CreateChildControls() if possible).

Once you do that, ASP.NET will automatically associate the input with the TextBox and you should be able to reference them as you normally would or via .FindControl() of the parent.



回答3:

I think You used big program for generating dynamic textboxes and inserting into data base.For retriving text from dynamically generated textboxes,use the code below..

Request.Form["Textbox" + i.ToString()] 

where "i" represents the number of textboxes you generated.

For more info.Please Check the below link.

how to insert value to sql db from dynamically generated textboxes asp.net