在datalist中的面板中查找控件

时间:2010-11-02 14:18:19

标签: c# asp.net

我有Datalist,在其中面板和内部面板标签,我想在datalist中的面板内找到标签。我做了我的代码,但它没有用好

protected void DataList1_ItemDataBound(object sender,DataListItemEventArgs e)     {

    Label LblHead = e.Item.FindControl("Label1") as Label;
    string LanguageID = Globals.GetSuitableLanguage(Page);

    if (LanguageID == "ar")
    {
        LblHead.Attributes.Add("CssClass", "hed_logo2");
    }
       }

2 个答案:

答案 0 :(得分:0)

添加以下检查,

if (e.Item.ItemType == ListItemType.Item || 
                 e.Item.ItemType == ListItemType.AlternatingItem)
{
    // your code here
}   

答案 1 :(得分:0)

此代码应该可以在我的本地测试:

protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        Panel panel1 = e.Item.FindControl("Panel1") as Panel;   //assume your panel name is Panel1
        if (panel1 != null)
        {
            Label LblHead = panel1.FindControl("LblHead") as Label;
            if (LblHead != null)
            {
                string LanguageID = Globals.GetSuitableLanguage(Page);
                if (LanguageID == "ar")
                {
                    LblHead.Attributes.Add("CssClass", "hed_logo2");
                }
            }
        }
    }
}