因为购买的货物种类不定
每一种货物对应表格的一行
刚开始的时候只有表头和第一行
处理的代码如下:
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处理的,究竟怎么做比较合理,恳请赐教,不胜感激!
18 个解决方案
#1
你把那个if (!Page.IsPostBack)去掉吧,
那个表示页面首次加载时执行,
而你点击后是页面回发了,
所以页面重新加载了。你去了试试!
那个表示页面首次加载时执行,
而你点击后是页面回发了,
所以页面重新加载了。你去了试试!
#2
去掉可以了,但是点击继续添加增加一行后,再点继续添加就没反应了,跟踪的时候发现table.rows.count还是2,应该是3了阿
#3
这样的话每次刷新页面都会执行createTableHead,所以一直是2行,应该怎么办啊
#4
其实,我的做法是,生成的时候,把这个Table存在Session里,添加的时候,把Table从Session里取出来,增加新行,在保存回去,同时,写到界面上去.
这里,你只要保持界面和Session同步就可以了.
这里,你只要保持界面和Session同步就可以了.
#5
在后台构造数据源dataset等,对数据源进行缓存
增加行时对dataset进行操作,然后重新根据数据源创建table即可
增加行时对dataset进行操作,然后重新根据数据源创建table即可
#6
顶
#7
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
#8
up
#9
先把一个htmltable或asp的table放在页面上.然后添加就可以保存状态了.
#10
先把一个htmltable或asp的table放在页面上.然后添加就可以保存状态了.
====================================================================
我这样做过,可是添加新行之后因为是重新创建的行,所以原来用户输入的数据都消失le
====================================================================
我这样做过,可是添加新行之后因为是重新创建的行,所以原来用户输入的数据都消失le
#11
这个是有点麻烦,
try
初始化用一个方法. 添加一行用一个方法如AddRow()
每按一次添加一行按纽 在ViewState["rowcount"] 中加1
在AddRow中从i=0 到 ViewState["rowcount"]
添加行.
每次page_load
createtable
AddRow
try
初始化用一个方法. 添加一行用一个方法如AddRow()
每按一次添加一行按纽 在ViewState["rowcount"] 中加1
在AddRow中从i=0 到 ViewState["rowcount"]
添加行.
每次page_load
createtable
AddRow
#12
private void Page_Load(object sender, System.EventArgs e)
{
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();
}
{
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();
}
#13
谢谢幕白兄,现在还有这样一个问题,如果用户添加了一行,输入数据后,再点添加新行,原来输入的数据还会存在吗?
#14
干脆把整个table放入viewState里面好了
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);
}
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);
}
#15
谢谢幕白兄,现在还有这样一个问题,如果用户添加了一行,输入数据后,再点添加新行,原来输入的数据还会存在吗?
=============================
幕白兄,这样做原来的数据还存在,我不太明白,既然是重新创建的行,为什么那些数据还能存在呢
=============================
幕白兄,这样做原来的数据还存在,我不太明白,既然是重新创建的行,为什么那些数据还能存在呢
#16
lindping(罗将神) ( ) 信誉:86 Blog
=====================================
这样也是个好主意,但是table里面的数据就丢失了
=====================================
这样也是个好主意,但是table里面的数据就丢失了
#17
幕白兄,这样做原来的数据还存在,我不太明白,既然是重新创建的行,为什么那些数据还能存在呢
=============
动态控件就是这样子的,虽然好像是重新创建的行,但只要位置相同,元素相同,asp.net会将他们当成同一控件.
=============
动态控件就是这样子的,虽然好像是重新创建的行,但只要位置相同,元素相同,asp.net会将他们当成同一控件.
#18
谢谢大家,问题搞定了,特别谢谢幕白兄:)
#1
你把那个if (!Page.IsPostBack)去掉吧,
那个表示页面首次加载时执行,
而你点击后是页面回发了,
所以页面重新加载了。你去了试试!
那个表示页面首次加载时执行,
而你点击后是页面回发了,
所以页面重新加载了。你去了试试!
#2
去掉可以了,但是点击继续添加增加一行后,再点继续添加就没反应了,跟踪的时候发现table.rows.count还是2,应该是3了阿
#3
这样的话每次刷新页面都会执行createTableHead,所以一直是2行,应该怎么办啊
#4
其实,我的做法是,生成的时候,把这个Table存在Session里,添加的时候,把Table从Session里取出来,增加新行,在保存回去,同时,写到界面上去.
这里,你只要保持界面和Session同步就可以了.
这里,你只要保持界面和Session同步就可以了.
#5
在后台构造数据源dataset等,对数据源进行缓存
增加行时对dataset进行操作,然后重新根据数据源创建table即可
增加行时对dataset进行操作,然后重新根据数据源创建table即可
#6
顶
#7
顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶
#8
up
#9
先把一个htmltable或asp的table放在页面上.然后添加就可以保存状态了.
#10
先把一个htmltable或asp的table放在页面上.然后添加就可以保存状态了.
====================================================================
我这样做过,可是添加新行之后因为是重新创建的行,所以原来用户输入的数据都消失le
====================================================================
我这样做过,可是添加新行之后因为是重新创建的行,所以原来用户输入的数据都消失le
#11
这个是有点麻烦,
try
初始化用一个方法. 添加一行用一个方法如AddRow()
每按一次添加一行按纽 在ViewState["rowcount"] 中加1
在AddRow中从i=0 到 ViewState["rowcount"]
添加行.
每次page_load
createtable
AddRow
try
初始化用一个方法. 添加一行用一个方法如AddRow()
每按一次添加一行按纽 在ViewState["rowcount"] 中加1
在AddRow中从i=0 到 ViewState["rowcount"]
添加行.
每次page_load
createtable
AddRow
#12
private void Page_Load(object sender, System.EventArgs e)
{
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();
}
{
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();
}
#13
谢谢幕白兄,现在还有这样一个问题,如果用户添加了一行,输入数据后,再点添加新行,原来输入的数据还会存在吗?
#14
干脆把整个table放入viewState里面好了
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);
}
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);
}
#15
谢谢幕白兄,现在还有这样一个问题,如果用户添加了一行,输入数据后,再点添加新行,原来输入的数据还会存在吗?
=============================
幕白兄,这样做原来的数据还存在,我不太明白,既然是重新创建的行,为什么那些数据还能存在呢
=============================
幕白兄,这样做原来的数据还存在,我不太明白,既然是重新创建的行,为什么那些数据还能存在呢
#16
lindping(罗将神) ( ) 信誉:86 Blog
=====================================
这样也是个好主意,但是table里面的数据就丢失了
=====================================
这样也是个好主意,但是table里面的数据就丢失了
#17
幕白兄,这样做原来的数据还存在,我不太明白,既然是重新创建的行,为什么那些数据还能存在呢
=============
动态控件就是这样子的,虽然好像是重新创建的行,但只要位置相同,元素相同,asp.net会将他们当成同一控件.
=============
动态控件就是这样子的,虽然好像是重新创建的行,但只要位置相同,元素相同,asp.net会将他们当成同一控件.
#18
谢谢大家,问题搞定了,特别谢谢幕白兄:)