帮我看看这个动态增加表格行的问题,谢谢!
我这是一个台帐登记的问题
因为购买的货物种类不定
每一种货物对应表格的一行
刚开始的时候只有表头和第一行
处理的代码如下:
private void CreateTableHead()
{
Table1.CellPadding = 0;
Table1.CellSpacing = 0;
Table1.Width = Unit.Percentage(100);
TableRow trHead = new TableRow();
TableCell tcHeadFoodName = new TableCell();
TableCell tcHeadType = new TableCell();
TableCell tcHeadNum = new TableCell();
tcHeadFoodName.Text = "食品名称";
tcHeadType.Text = "规格";
tcHeadNum.Text = "数量";
trHead.Cells.Add(tcHeadFoodName);
trHead.Cells.Add(tcHeadType);
trHead.Cells.Add(tcHeadNum);
trHead.HorizontalAlign = HorizontalAlign.Center;
trHead.BackColor = Color.AliceBlue;
trHead.Height = 30;
Table1.Rows.Add(trHead);
TableRow tr1 = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();
TextBox tb1 = new TextBox();
tb1.ID = "tbFoodName" + Table1.Rows.Count;
TextBox tb2 = new TextBox();
tb2.ID = "tbType" + Table1.Rows.Count;
TextBox tb3 = new TextBox();
tb3.ID = "tbNum" + Table1.Rows.Count;
tc1.Controls.Add(tb1);
tc2.Controls.Add(tb2);
tc3.Controls.Add(tb3);
tr1.Cells.Add(tc1);
tr1.Cells.Add(tc2);
tr1.Cells.Add(tc3);
tr1.HorizontalAlign = HorizontalAlign.Center;
tr1.Height = 30;
tr1.VerticalAlign = VerticalAlign.Middle;
Table1.Rows.Add(tr1);
}
现在我的想法是点击添加新行的时候,就自动添加一行,处理代码如下:
private void btnNewRow_Click(object sender, System.EventArgs e)
{
TableRow tr = new TableRow();
TableCell tc1 = new TableCell();
TableCell tc2 = new TableCell();
TableCell tc3 = new TableCell();
TextBox tb1 = new TextBox();
tb1.ID = "tbFoodName" + Table1.Rows.Count;
TextBox tb2 = new TextBox();
tb2.ID = "tbType" + Table1.Rows.Count;
TextBox tb3 = new TextBox();
tb2.ID = "tbNum" + Table1.Rows.Count;
tc1.Controls.Add(tb1);
tc2.Controls.Add(tb2);
tc3.Controls.Add(tb3);
tr.Cells.Add(tc1);
tr.Cells.Add(tc2);
tr.Cells.Add(tc3);
Table1.Rows.Add(tr);
}
因为在页面加载时我是这样处理的:
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString.Count != 0)
{ CreateTableHead();
}
}
}
现在的问题是当我点击添加薪行的时候,原来的表头就不见了,因为页面刷新的时候不会执行!ispostback里面的代码了,应该如何解决,而且添加新行之后,原来的行输入的内容不会丢失,我看也有用javascript处理的,究竟怎么做比较合理,恳请赐教,不胜感激!