从动态文本框中获取用户输入

时间:2013-08-09 18:39:36

标签: c# asp.net textbox

我有两个buttons。一个按钮创建Textbox,另一个按钮提交信息。创建textbox后,我无法检索用户文本。这是代码:

 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

    }

提前谢谢!

3 个答案:

答案 0 :(得分:2)

以下是动态创建TextBox的方法。它跟踪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
    }
}

答案 1 :(得分:0)

不要将TextBox存储在Session中;相反,在页面上创建它们。

诀窍是每次在适当的时间创建它们(即每个PostBack)。尝试为页面加载OnLoad()(如果可能的话,加载CreateChildControls())。

一旦这样做,ASP.NET将自动将输入与TextBox相关联,您应该能够像往常一样或通过父级的.FindControl()引用它们。

答案 2 :(得分:0)

我认为您使用大程序生成动态文本框并插入数据库。要从动态生成的文本框中检索文本,请使用下面的代码。

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

其中“i”表示您生成的文本框数。

欲了解更多信息,请查看以下链接。

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