在datalist中找到tr的id

时间:2010-11-09 11:38:37

标签: c# asp.net

我有桌子无家可归者,我必须在表格中找到tr的id我做了我的代码但是错误apeared(对象引用...)。

ASPX:

<asp:DataList ID="DataList2" runat="server" RepeatColumns="4" 
    RepeatDirection="Horizontal" onitemdatabound="DataList2_ItemDataBound">
    <ItemTemplate>

                    <table width="90%" border="0" cellspacing="0" cellpadding="2">
                        <tr id="AllText" runat="server">
                            <td class="media_ved_tit_aro" id="AllArrow" runat="server">
                                &nbsp;
                            </td>
                            <td class="media_txt_b " >
                            <asp:Label ID="Label4" runat="server" Text='<%#Eval(Globals.Translate("YoutubeTitle_EN", Page))%>' />
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </ItemTemplate>
</asp:DataList>

CS:

protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
{
    string LanguageID = Globals.GetSuitableLanguage(Page);
       if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
       {
           ArrowAll  = e.Item.FindControl("AllArrow") as HtmlGenericControl;
           TextAll = e.Item.FindControl("AllText") as HtmlGenericControl;
       }
       if (LanguageID == "ar")
       {

           ArrowAll.Attributes.Add("Class", "media_ved_tit_aro_AR");
           TextAll.Attributes.Add("Class", "News_Image");
       }
}

1 个答案:

答案 0 :(得分:0)

tr,td不会映射到HtmlGenericControl,而是映射到HtmlTableRowHtmlTableCell。我更喜欢使用强制转换来控制基础 - HtmlControl。简而言之,如下所示更改代码片段:

ArrowAll = e.Item.FindControl("AllArrow") as HtmlControl; 
TextAll = e.Item.FindControl("AllText") as HtmlControl;