我这是一个台帐登记的问题
因为购买的货物种类不定
每一种货物对应表格的一行
刚开始的时候只有表头和第一行
处理的代码如下:
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处理的,究竟怎么做比较合理,恳请赐教,不胜感激!
因为购买的货物种类不定
每一种货物对应表格的一行
刚开始的时候只有表头和第一行
处理的代码如下:
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处理的,究竟怎么做比较合理,恳请赐教,不胜感激!
那个表示页面首次加载时执行,
而你点击后是页面回发了,
所以页面重新加载了。你去了试试!
增加行时对dataset进行操作,然后重新根据数据源创建table即可
====================================================================
我这样做过,可是添加新行之后因为是重新创建的行,所以原来用户输入的数据都消失le
try初始化用一个方法. 添加一行用一个方法如AddRow()每按一次添加一行按纽 在ViewState["rowcount"] 中加1在AddRow中从i=0 到 ViewState["rowcount"]
添加行.每次page_loadcreatetable
AddRow
{
this.CreateTableHead();
for(int i=0;i<this.NewRowCount;i++)
{
this.AddRow();
} }
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 AddRow()
{
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 int NewRowCount
{
get
{
return ViewState["RowCount"] == null?0:(int)ViewState["RowCount"];
}
set
{
ViewState["RowCount"] = value;
}
}
private void btnNewRow_Click(object sender, System.EventArgs e)
{
this.NewRowCount ++;
this.AddRow();
}
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString.Count != 0)
{
CreateTableHead();
ViewSate["Table1"] = Table1;
}
}
}
....private void btnNewRow_Click(object sender, System.EventArgs e)
{
...
Table Table1 = (Table)ViewState["Table1"];
Table1.Rows.Add(tr);
}
=====================================
这样也是个好主意,但是table里面的数据就丢失了
=============
动态控件就是这样子的,虽然好像是重新创建的行,但只要位置相同,元素相同,asp.net会将他们当成同一控件.