In a Datalist, onitembound I apply style to a div based on a condition with
C#
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?


Jignesh KumarRank #91 · 22.9K reputationPosted Sep 28, 2024, 5:15 AM EDT
Hello Marius,
If your text box name starts with txt... then you can find the below,
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) { for (int i = 1; i <= 20; i++) { // Dynamically get the control name like txt1, txt2, txt3 string controlId = $"txt{i}"; // Find the control TextBox txt = (TextBox)e.Item.FindControl(controlId); if (txt != null) { // Apply the style to the control txt.Attributes.Add("style", "background-color: Green; color: white; font-size: 9px"); } } } } }Marius VasileRank #785 · 2.9K reputationPosted Sep 28, 2024, 5:30 AM EDT
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
protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { for (int i = 1; i <= 9; i++) { // Dynamically get the control name like txt1, txt2, txt3 string controlId = $"txt{i}"; int r = int.Parse(controlId.ToString()); // Find the control TextBox txt = (TextBox)e.Item.FindControl(controlId); if (txt != null && r < 5) { // Apply the style to the control txt.Attributes.Add("style", "background-color: Green; color: white; font-size: 9px"); } } } }but I get the error "Input string was not in a correct format."
Jignesh KumarRank #91 · 22.9K reputationPosted Sep 28, 2024, 5:57 AM EDT
protected void DataList2_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { for (int i =1; i <=5; i++) { // Dynamically get the control name like txt1, txt2, txt3 string controlId = $"txt{i}"; // Find the control TextBox txt = (TextBox)e.Item.FindControl(controlId); if (txt != null && i <= 5) { // Apply the style to the control txt.Attributes.Add("style", "background-color: Green; color: white; font-size: 9px"); } } } }Marius VasileRank #785 · 2.9K reputationPosted Sep 28, 2024, 6:09 AM EDT
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
DataRowView drv = (DataRowView)(e.Item.DataItem); int rating = int.Parse(drv.Row["U1"].ToString()); if (rating > 0) { TextBox txt = (TextBox)e.Item.FindControl("txt1"); txt.Attributes.Add("style", "background-color: Green;color:white;font-size:9px"); }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 KumarRank #91 · 22.9K reputationPosted Sep 28, 2024, 7:27 AM EDT
ok now you got the answer for your question right ?
Marius VasileRank #785 · 2.9K reputationPosted Sep 28, 2024, 7:56 AM EDT
I do not have the right answer, sorry. Not sure there is one, though