开发者

find control in panel in datalist

I had Datalist and inside it panel and inside panel label and I want to find label inside panel inside datalist.I did my code but it didnot work well

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");
    }
       }


This code should work, tested on my local:

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");
                }
            }
        }
    }
}


add following check,

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜