开发者

HTMLtableCell is not working for table header (th) in asp.net

I have repeater control that have table with table header(th) and table cell(td). I want to access td and th in code. I use HTMLTableCell for both in repeater code, I can access the td, but th is not working. Any suggestions Here is the code 开发者_JS百科sample:

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

This code works for td1 (which is cell) but object null reference exception on header1 Sorry, I do have runat="server" for th


You need to add a runat="server" attribute if you want to access it in code-behind.


The problem is that e.Item.FindControl("header1") will not return anything, because your header1 table header isn't part of e.Item (which essentially contains the rendered contents of the ItemTemplate). You need to be searching the control tree for the Repeater, not the individual item.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜