我正在尝试使用Web窗体中的Entity Framework将值从动态文本框插入到数据库中。 只需单击一下,它就会生成一个包含三列的表格行,每列都有一个文本框。每隔一次点击就更多一行。
public static int rowCnt = 0;
protected void BtnAddNewBuildItem_Click(object sender, EventArgs e)
{
// Current row count.
int rowCtr;
// Total number of cells per row (columns).
int cellCtr;
// Current cell counter
int cellCnt;
rowCnt = rowCnt + 1;
cellCnt = 3;
for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
{
// Create new row and add it to the table.
TableRow tRow = new TableRow();
TableBuildItems.Rows.Add(tRow);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
TextBox tb = new TextBox();
// Set a unique ID for each TextBox added
tb.ID = "txtBuisniesItem_Row" + rowCtr + "Cell" + cellCtr;
// Add the control to the TableCell
tCell.Controls.Add(tb);
tRow.Cells.Add(tCell);
}
}
}
然后,在另一个按钮上单击我试图将一行值插入数据库,但总是空字符串。
using (VODOMONTEntities context = new VODOMONTEntities())
{
BuildItem bi = new BuildItem();
for (int i = 0; i < rowCnt; i++)
{
TextBox tb1 = new TextBox();
tb1.ID = "txtBuisniesItem_Row" + (i + 1).ToString() + "Cell" + 1;
bi.Name = tb1.Text; //there is a empty string allways
context.BuildItems.Add(bi);
}
context.SaveChanges();
}
所以,我的问题是如何将动态texbox值插入数据库,就像在这种情况下一样? 谢谢!
答案 0 :(得分:1)
protected void CreateNewBuild_Click(object sender, EventArgs e)
{
using (VODOMONTEntities context = new VODOMONTEntities())
{
Build b = new Build();
BuildItem bi = new BuildItem();
b.BuildSubject = txbSubject.Text;
b.BuildNumber = txbBuildNumber.Text;
b.City = txbCityBuild.Text;
// b.BuildDate = txbBuildDate.Text;
b.ClientId = Int32.Parse(ddlClient.SelectedValue);
context.Builds.Add(b);
for (int i = 0; i < (RowIndexNumber-1)/3; i++)
{
TextBox[] tb = new TextBox[RowIndexNumber];
tb[i*3] = new TextBox();
tb[i*3] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 1).ToString());
bi.Name = tb[i*3].Text;
tb[i*3+1] = new TextBox();
tb[i*3+1] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 2).ToString());
bi.Quantity = Convert.ToInt32(tb[i * 3 + 1].Text);
tb[i*3+2] = new TextBox();
tb[i*3+2] = (TextBox)FindControl("txtBuisniesItem_Row" + (i * 3 + 3).ToString());
bi.Price = decimal.Parse(tb[i * 3 + 2].Text.ToString());
bi.Build = b;
context.BuildItems.Add(bi);
context.SaveChanges();
}
}
}
public int RowIndexNumber = 1;
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtBuisniesItem_Row")).ToList();
foreach (string key in keys)
{
this.CreateTextBox("txtBuisniesItem_Row" + RowIndexNumber);
if (RowIndexNumber % 3 == 0)
{
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
RowIndexNumber++;
}
}
protected void BtnAddNewBuildItem_Click(object sender, EventArgs e)
{
int index = pnlTextBoxes.Controls.OfType<TextBox>().ToList().Count + 1;
this.CreateTextBox("txtBuisniesItem_Row" + index);
this.CreateTextBox("txtBuisniesItem_Row" + (index + 1).ToString());
this.CreateTextBox("txtBuisniesItem_Row" + (index + 2).ToString());
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBoxes.Controls.Add(lt);
}
private void CreateTextBox(string id)
{
TextBox txt = new TextBox();
txt.ID = id;
txt.Attributes.Add("runat", "Server");
pnlTextBoxes.Controls.Add(txt);
}