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?

Marius VasilePosted Sep 28, 2024, 11:56 AM
I do not have the right answer, sorry. Not sure there is one, though
Jignesh KumarPosted Sep 28, 2024, 11:27 AM
ok now you got the answer for your question right ?
Marius VasilePosted Sep 28, 2024, 10:09 AM
Sorry Jignesh, I mislead you. Each txt1, txt2...txt21 has a value, actually is a sum of the column but it's not important for my case. What I want to do is to color green the div that has a value less than 5.
With
I get the value for each div and apply style. I was hoping for a way to check all divs (txt1, txt2,...txt21), one by one and apply style for those meeting the criteria
Jignesh KumarPosted Sep 28, 2024, 9:57 AM
Marius VasilePosted Sep 28, 2024, 9:30 AM
Thank you Jignesh for your answer, what I need is to check the value for each txt1..21 box and if is between 0 and 5 to have a color.
I tried your code buy having
but I get the error "Input string was not in a correct format."
Jignesh KumarPosted Sep 28, 2024, 9:15 AM
Hello Marius,
If your text box name starts with txt... then you can find the below,