【发布时间】:2014-11-03 09:34:14
【问题描述】:
这是我创建动态文本框的代码..我想将这些值插入数据库
我需要将动态创建的文本框值插入到数据库中。我该怎么做..我是 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);
}
}
【问题讨论】:
-
您使用的是什么数据库,您是如何与之通信的?
-
我正在使用 ms sql server 数据库..
-
我的表名是Category,字段是CategoryID,CategoryName
-
我强烈反对在 ASP.NET Web 表单应用程序中生成动态控件,因为在回发期间保留它们总是很痛苦。如果你真的需要提供这样的功能,那就继续使用 ASP.NET MVC 甚至 ASP.NET MVC SPA,生活会更轻松,你的代码也可以管理