动态地向表中添加行 [英] Add rows to a table dynamically

查看:102
本文介绍了动态地向表中添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在尝试创建一个表,其内容应该由子类创建(查询RESTful Web服务的结果)。我现在已经工作了很长一段时间,我似乎无法让它发挥作用。我尝试了很多不同的解决方案。

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). 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.


  • 在子类中创建表并将其添加到Page.Controls。这让我觉得控件集合无法修改,因为控件包含代码块(即<%...%>)。这根本没有意义。

  • 我试图在页面上创建一个空表,并将其句柄传递给负责添加行的子类。注意发生了。

  • 我试图返回一个TableRowCollection并将其分配给之前创建的(空)表。什么都没发生。

  • 现在我只想在表格中添加一行和一个单元格(宝贝迈向我需要的东西)。甚至没有用。请找到附件的代码:

  • creating the table in the subclass and add it to Page.Controls. That gets me "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." which does not make sense at all.
  • I have tried to create an empty table on the page and passed its handle to the subclass which was responsible for adding rows. Noting happened.
  • I have tried to return a TableRowCollection and assigned it to the previously created (empty) table. Nothing happened.
  • 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());


表格简单而空白:

<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.

推荐答案

它正在运行,我已经测试过了:

在我的页面加载中,我有:

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);
        }

在我的aspx页面上,我有:

On my aspx page, I have:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:Table ID="table" runat="server" /> 
</asp:Content>

此代码呈现的html:

The html rendered by this code:

<table id="MainContent_table">
    <tbody><tr>
        <td><input name="ctl00$MainContent$ctl00" type="text"></td>
    </tr>
</tbody></table>

这篇关于动态地向表中添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆