[英]Add rows to a table dynamically
At the moment I am trying to create a table whose content is supposed to be created by a subclass (result of querying a RESTful web service). 目前我正在尝试创建一个表,其内容应该由子类创建(查询RESTful Web服务的结果)。 I have been working for quite some time on that now and I just cannot seem to get it to work. 我现在已经工作了很长一段时间,我似乎无法让它发挥作用。 I have tried so many different solutions. 我尝试了很多不同的解决方案。
Now I just want to add one row and one cell to the table (baby steps towards what I need). 现在我只想在表格中添加一行和一个单元格(宝贝迈向我需要的东西)。 Not even that works. 甚至不行。 Please find the code for that attached: 请找到附件的代码:
TableRow row = new TableRow(); table.Rows.Add(row); TableCell cell = new TableCell(); row.Cells.Add(cell); cell.Controls.Add(new TextBox());
The table is simple and empty: 该表简单而空洞:
<asp:Table ID="table" runat="server" />
The source code that is displayed by my browser looks like that: 我的浏览器显示的源代码如下所示:
<table id="table">
</table>
I have been looking at countless examples on the web and all look like this. 我一直在网上看到无数的例子,看起来都像这样。 I guess it is only a tiny problem somewhere but I have not been able to figure it out. 我想这在某个地方只是一个小问题,但我无法弄明白。 Now I am willing to offer life-long gratitude to everybody who can provide the hint for solving this mess. 现在,我愿意向能够提供解决这一混乱局面的人提供终生的感激之情。
It is working, I have tested it: 它正在工作,我测试过它:
In my page load, I have: 在我的页面加载中,我有:
protected void Page_Load(object sender, EventArgs e)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Controls.Add(new TextBox());
row.Cells.Add(cell);
table.Rows.Add(row);
}
On my aspx page, I have: 在我的aspx页面上,我有:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Table ID="table" runat="server" />
</asp:Content>
The html rendered by this code: 这段代码呈现的html:
<table id="MainContent_table">
<tbody><tr>
<td><input name="ctl00$MainContent$ctl00" type="text"></td>
</tr>
</tbody></table>
I would use either an asp:GridView or an asp:Repeater 我会使用asp:GridView或asp:Repeater
eg with Repeater 例如使用Repeater
<table>
<asp:Repeater id="repeater1" runat="server">
<ItemTemplate>
<tr>
<td><asp:Literal id="literal1" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
then in your code behind 然后在你的代码后面
repeater1.DataSource = myDatasource;
repeater1.DataBind();
or you could use a GridView 或者您可以使用GridView
using table.Rows.Add() will tend to cause you problems, particularly with disappearing content on postback, or problems with event handlers not firing should you need to add any LinkButtons or anything like that to your table cells 使用table.Rows.Add()会导致问题,特别是在回发时内容消失,或者事件处理程序没有触发的问题,如果你需要向表格单元格添加任何LinkButtons或类似的东西
The question is where are you adding these rows in the table, I mean in which event of page? 问题是你在表中添加这些行的位置,我的意思是页面的哪个事件?
As I feel that post back is clearing all the added rows. 因为我觉得回发后正在清除所有添加的行。
Kindly tell the execution sequence and also try to put your code in page_init event for adding rows. 请告诉执行顺序,并尝试将您的代码放在page_init事件中以添加行。
Might solve your problem.
Make table runat="server" in your aspx code
<table id="tbl" runat="server">
</table>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
// Add the TableRow to the Table
tbl.Rows.Add(tr);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.