【问题标题】:Add rows to a table dynamically动态向表中添加行
【发布时间】:2012-06-02 11:18:23
【问题描述】:

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

  • 在子类中创建表并将其添加到 Page.Controls。这让我“无法修改 Controls 集合,因为控件包含代码块(即 )。”这根本没有意义。
  • 我试图在页面上创建一个空表并将其句柄传递给负责添加行的子类。注意到发生了。
  • 我已尝试返回 TableRowCollection 并将其分配给先前创建的(空)表。什么也没发生。
  • 现在我只想在表格中添加一行和一个单元格(朝着我需要的方向迈出一步)。即使这样也行不通。请找到所附代码:

    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" /> 

我的浏览器显示的源代码如下所示:

<table id="table">
</table> 

我一直在网络上查看无数示例,所有示例都是这样的。我想这只是某个地方的一个小问题,但我一直无法弄清楚。现在,我愿意向所有能够为解决这个烂摊子提供线索的人表示终生的感谢。

【问题讨论】:

  • 我已经编辑了代码,请检查...

标签: asp.net html-table


【解决方案1】:

它工作正常,我已经测试过了:

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

 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 页面上,我有:

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

这段代码渲染的html:

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

【讨论】:

  • 还是什么都没有。它不应该有所作为,因为它只会将对象的引用传递给父对象,而不是对象本身。
【解决方案2】:

我会使用 asp:GridView 或 asp:Repeater

例如使用中继器

    <table>
        <asp:Repeater id="repeater1" runat="server">
            <ItemTemplate>
                <tr>
                    <td><asp:Literal id="literal1" runat="server" /></td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>

然后在你的代码后面

repeater1.DataSource = myDatasource;
repeater1.DataBind();

或者你可以使用 GridView

使用 table.Rows.Add() 往往会给您带来问题,特别是回发时内容消失,或者如果您需要将任何 LinkBut​​tons 或类似的东西添加到表格单元格中,事件处理程序无法触发的问题

【讨论】:

    【解决方案3】:

    问题是您将这些行添加到表中的什么位置,我的意思是在哪个页面事件中?

    我觉得回发正在清除所有添加的行。

    请告知执行顺序,并尝试将代码放入 page_init 事件中以添加行。

    【讨论】:

    • 创建行的代码在我创建表后直接在代码块中。所以我希望它在页面加载时执行。该代码块中的其他语句实际上已被执行。
    【解决方案4】:
    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);
                      }
    
            }
    

    【讨论】:

    • 我为表设置了 runat="server" 属性。不会改变任何东西。
    • 你在后面的代码中访问表ID,然后使用上面的代码。它工作得很好,我已经测试过了。。
    • 您必须在页面加载或任何事件触发后生成表格
    • 嘿,是的!这样可行。所以我已将代码转移到 Page_Load 并且有效。感谢那!但是为什么不能放在aspx文件的一个代码块里呢?
    • @jan:我没听懂你说的“但是为什么我不能把它放在 aspx 文件的代码块中?”
    猜你喜欢
    • 1970-01-01
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2019-02-12
    • 1970-01-01
    • 2019-06-24
    相关资源
    最近更新 更多