我的 aspx 页面上有一个 asp.net 表控件,包含
asp.net TableRow 对象;
我的目的是通过单击某个图像按钮动态地将列添加到该表中。
该表由多个 TableRow 对象组成,每个对象默认包含一个带有某些标签的 ASP.NET TableCell。我试图通过单击图像按钮来添加列,据说将一个新单元格添加到该行的单元格集合中。例如,如果我想添加一列,其中包含特定行的下拉列表,我会这样做:
DropDownList ddl = new DropDownList();
TableCell tableCell= new TableCell();
tableCell.Controls.Add(ddl);
tableRow.Cells.Add(tableCell);
问题是我永远不能超出一个添加的列,因为每当我单击我创建的 asp.net“AddColumn”按钮时,在我到达生活中某个地方的处理程序之前
循环 TableRow 对象及其单元格集合消亡。
我考虑过以某种方式保存会话中行对象的副本,但我不认为
进行保存的正确时间点。
无论如何,asp.net 不是应该将所有内容都保持在视图状态吗?请帮忙并提供解决方案。我完全糊涂了。
顺便说一句,ASPX 看起来像这样:
<asp:TableRow runat="server" ID="RequestorEmailRow">
<asp:TableCell runat="server" Text="...">
</asp:TableCell>
</asp:TableRow>
I've have on my aspx page an asp.net table control, containing
asp.net TableRow objects;
My purpose is to dynamically add columns to this table by clicking some image button.
The table consists of several TableRow objects, each containing by default an ASP.NET TableCell with some label. I am trying to add the columns by clicking the image button, supposedly adding a new cell to the cell collection of the row. For example if I want to add a column containing a dropdown list for a specific row, I do:
DropDownList ddl = new DropDownList();
TableCell tableCell= new TableCell();
tableCell.Controls.Add(ddl);
tableRow.Cells.Add(tableCell);
The problem is that I can never go beyond one added column, because whenever I click the asp.net "AddColumn" button I've created, before I get to its handler somewhere in the life
cycle the TableRow object dies, along with its collection of cells.
I've thought about saving copies of the row objects in session somehow, but I can't think
of a correct point in time to do the saving.
And anyhow - isn't the asp.net supposed to keep everything in viewstate? Please help and provide solutions. I'm totally confused.
The ASPX btw looks something like this:
<asp:TableRow runat="server" ID="RequestorEmailRow">
<asp:TableCell runat="server" Text="...">
</asp:TableCell>
</asp:TableRow>
发布评论
评论(1)
ASP.NET 视图状态仅存储控件的值,而不存储控件本身的存在。视图状态在 样式对象动态添加到网页,则必须在
Page_Init
和Page_Load
之间反序列化。如果将Page_Init
中重新创建它们,否则 ASP.NET 将不会处理它们的视图状态。更好的方法是使用
GridView
控件(位于工具箱的“数据”部分)。与asp:Table
不同,asp:GridView< /code> 的视图状态包括您可能添加的任何行。
The ASP.NET viewstate only stores the values of controls, not the presence of controls themselves. The viewstate is deserialized between
Page_Init
andPage_Load
. If you dynamically add<asp:
style objects to a web page, you have to recreate them inPage_Init
, or ASP.NET will not process their viewstate.A nicer approach would be to use a
GridView
control (in the "Data" section of the toolbox.) Unlike theasp:Table
, theasp:GridView
's viewstate includes any rows you might have added.