简体   繁体   English

如何以编程方式在aspx页面中创建多个文本框并保存到数据库?

[英]How to Programatically create Multiple textboxes in an aspx page and save to database?

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. 我试图通过允许用户在文本框中键入(例如,4)并按下按钮将这些填充到面板中来以编程方式填充文本框(txtFirstName,txtSecondName)。 So if they put 2 in then they will get 2 rows of textboxes for first and last name. 因此,如果他们输入2,那么他们将获得2行文字框的名字和姓氏。 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();
    }                
}

You can get the post parametres using the Request.Form . 您可以使用Request.Form获取post参数。

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: 发布的参数是使用渲染的html元素的name值,或服务器端的UniqueID ,但因为您无法在服务器端设置UniqueID ,并且因为您动态地创建它们,所以可能nameid相同,至少在我的测试中,该行将如下:

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. 为了澄清,通常你需要做这个Request.Form[control.UniqueID]来获取发布的值,但是因为你没有control因为你动态地创建它并且不再存在,接下来是获取帖子使用已发布的名称的值,因此发布的名称是这个"txtFirstName" + i.ToString()的方式。

relative: 相对的:
asp.net request.form asp.net request.form
Accessing control client name and not ID in ASP.NET 在ASP.NET中访问控制客户端名称而不是ID
Get info about Http Post field order 获取有关Http Post现场订单的信息

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 您可以浏览此链接以了解如何保留动态创建的控件的状态: http//www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM