Marius Vasile

Marius Vasile

  • 601
  • 1.9k
  • 143.1k

asp.net c# Change style for all rows inside a datalist on condition

Sep 28 2024 7:45 AM

In a Datalist, onitembound I apply style to a div based on a condition with

 

protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        DataRowView drv = (DataRowView)(e.Item.DataItem);
        int rating = int.Parse(drv.Row["A1"].ToString());

        if (rating > 0 && rating < 5)
        {
            TextBox txt = (TextBox)e.Item.FindControl("txtA1");

            txt.Attributes.Add("style", "background-color: Green;color:white;font-size:9px");

        }
    }
}

but in the Datalist I have 21 controls (txtA1 .... txtA20). Is there a way to apply style for all divs that meet the condition?


Answers (6)