如何将动态创建的文本框值插入数据库

时间:2014-11-03 09:34:14

标签: c# asp.net

这是我创建动态文本框的代码。我想将这些值插入数据库

我需要将动态创建的文本框值插入到database.how我可以这样做..我是asp.net的新手

标记:

<div>
    <asp:Panel ID="Panel1" runat="server" Width="300px" />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>

代码隐藏:

protected void Page_load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //Remove the session when first time page loads.
        Session.Remove("clicks");
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    int rowCount = 0;

    //initialize a session.
    rowCount = Convert.ToInt32(Session["clicks"]);

    rowCount++;

    //In each button clic save the numbers into the session.
    Session["clicks"] = rowCount;

    //Create the textboxes and labels each time the button is clicked.
    for (int i = 0; i < rowCount; i++)
    {
        TextBox TxtBoxU = new TextBox();          
        Label lblU = new Label();          
        TxtBoxU.ID = "TextBoxU" + i.ToString();            
        lblU.ID = "LabelU" + i.ToString();         
        lblU.Text = "Category Name " + (i + 1).ToString() + " : ";

        //Add the labels and textboxes to the Panel.
        Panel1.Controls.Add(lblU);
        Panel1.Controls.Add(TxtBoxU);
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您无法访问textbox的值,通过这个您可以轻松访问该值。    我知道如何将数据插入数据库。

   for (int i = 0; i < rowCount; i++)
            {
                string OptionID = "TextBoxU" + i;
                TextBox tb = (TextBox)Panel1.FindControl(OptionID);

             /******** USe the tb.Text value to get 
                    the value and then store it into Database**********/
            }