You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ASP.NET中将HTMLButton添加到HTMLTableCell中

解决ASP.NET动态表格中添加HtmlButton到HtmlTableCell的问题

你只需要对现有代码做两处关键修改,就能把按钮成功添加到表格单元格里:

首先,HtmlTableCell作为容器控件,它的Controls属性可以用来添加子控件(比如你的HtmlButton);其次,要把包含按钮的单元格添加到当前行的单元格集合中。

修改后的代码片段:

for (int i = 0; i < dokuDaten.Count; i++) { 
    row = new HtmlTableRow(); 
    // ... 省略之前创建其他单元格的代码 ...

    //Button that I want to implement into the cell 
    HtmlButton zuordnungsButton = new HtmlButton(); 
    zuordnungsButton.ID = "btnZuordnung_" + i; // 建议给每个按钮设置唯一ID,避免回发冲突
    zuordnungsButton.InnerText = "Zuordnen"; 
    zuordnungsButton.Attributes.Add("Class","btn btn-success"); 
    zuordnungsButton.ServerClick += SaveButton_Click; 

    //The cell where the button should appear in the end 
    HtmlTableCell cellOptionen = new HtmlTableCell(); 
    // 1. 将按钮添加到单元格的Controls集合
    cellOptionen.Controls.Add(zuordnungsButton);
    // 2. 将包含按钮的单元格添加到当前行
    row.Cells.Add(cellOptionen);

    tbl.Rows.Add(row); 
} 
TDPlaceHolder.Controls.Add(tbl); 

关键说明:

  • 给每个按钮设置唯一ID(比如拼接循环变量i)是很重要的,这样在回发时ASP.NET能正确识别哪个按钮触发了事件,避免出现事件绑定混乱的问题。
  • Controls.Add()是ASP.NET服务器控件添加子控件的标准方式,所有容器类控件(比如HtmlTableCell、HtmlTableRow、PlaceHolder)都支持这个方法。

这样修改后,你的"Zuordnen"按钮就会出现在每一行的最后一个单元格里了。

内容的提问来源于stack exchange,提问作者Davis Jahn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 16:47:53