【问题标题】:HTMLtableCell is not working for table header (th) in asp.netHTMLtableCell 不适用于 asp.net 中的表头(th)
【发布时间】:2011-07-14 17:44:06
【问题描述】:

我有中继器控件,该控件具有带有表头(th)和表单元格(td)的表。我想在代码中访问 td 和 th 。我在转发器代码中都使用 HTMLTableCell,我可以访问 td,但它不起作用。有什么建议么 这是代码示例:

<asp:Repeater ID="rpt" runat="server" OnItemDataBound="repeater_ItemDataBound" >
<HeaderTemplate>
    <table id="tbl" >
    <thead>
    <tr>
      <th id="header1" runat="server">head 1/th>
      <th">Head 2</th>
 </tr>
    </thead>  
</HeaderTemplate>
<ItemTemplate>
    <tr>
      <td id="td1" runat="server" ><asp:Literal ID="litTD1" runat="server" /></td>
      <td  ><asp:Literal ID="litTD2" runat="server" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
    </table>
</FooterTemplate>
</asp:Repeater>

in code behind

HtmlTableCell header1= e.Item.FindControl("header1") as HtmlTableCell;
HtmlTableCell td1= e.Item.FindControl("td1") as HtmlTableCell;

此代码适用于 td1(即单元格),但 header1 上的对象空引用异常 抱歉,我确实有 runat="server"

【问题讨论】:

  • 应该只有一个表头...不应该重复。查看源代码并查看正在呈现的内容?

标签: asp.net


【解决方案1】:

如果你想在代码隐藏中访问它,你需要添加一个runat="server" 属性。

【讨论】:

  • 抱歉打错了,我确实有 runat="server" 属性
【解决方案2】:

问题是e.Item.FindControl("header1") 不会返回任何内容,因为您的header1 表头不是e.Item 的一部分(它本质上包含ItemTemplate 的渲染内容)。您需要在控制树中搜索中继器,而不是单个项目。

【讨论】:

  • 我应该如何处理?你能给我一个例子或示例代码,或者解释一下。
猜你喜欢
  • 1970-01-01
  • 2017-06-12
  • 2015-08-18
  • 2020-02-14
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 2012-03-12
  • 2018-01-16
相关资源
最近更新 更多