Hi akhter,
You need to make use of OnItemDataBound event handler of DataList control.
Inside the OnItemDataBound event handler check the condition and apply BgColor to TableCell.
Refer below sample and modify as per your requirement.
HTML
<asp:DataList ID="dlCustomers" runat="server" RepeatLayout="Table" OnItemDataBound="dlCustomers_OnItemDataBound">
<HeaderTemplate>
<table class="table">
<tr>
<td>
TDID
</td>
<td>
TDDesc
</td>
<td>
taskdate
</td>
<td>
enddate
</td>
<td>
TDstatus
</td>
<td>
latedays
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("TDID")%>
</td>
<td>
<%# Eval("TDDesc")%>
</td>
<td>
<%# Eval("taskdate")%>
</td>
<td>
<%# Eval("enddate")%>
</td>
<td>
<%# Eval("TDstatus")%>
</td>
<td runat="server" id="tbl">
<asp:Label ID="lblLateDays" Text='<%# Eval("latedays")%>' runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
Namespaces
C#
using System.Data;
using System.Web.UI.HtmlControls;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("TDID");
dt.Columns.Add("TDDesc");
dt.Columns.Add("taskdate");
dt.Columns.Add("enddate");
dt.Columns.Add("TDstatus");
dt.Columns.Add("latedays");
dt.Rows.Add(1, "hey", "01/02/2019", "04/05/2019", "Pending", "1");
dt.Rows.Add(2, "Hi", "05/03/2019", "08/06/2019", "USA", "0");
this.dlCustomers.DataSource = dt;
this.dlCustomers.DataBind();
}
}
protected void dlCustomers_OnItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataListItem item = e.Item as DataListItem;
string lateDays = (item.FindControl("lblLateDays") as Label).Text.Trim();
if (lateDays == "0")
{
HtmlTableCell tbl = item.FindControl("tbl") as HtmlTableCell;
tbl.BgColor = "Red";
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add("TDID")
dt.Columns.Add("TDDesc")
dt.Columns.Add("taskdate")
dt.Columns.Add("enddate")
dt.Columns.Add("TDstatus")
dt.Columns.Add("latedays")
dt.Rows.Add(1, "hey", "01/02/2019", "04/05/2019", "Pending", "1")
dt.Rows.Add(2, "Hi", "05/03/2019", "08/06/2019", "USA", "0")
Me.dlCustomers.DataSource = dt
Me.dlCustomers.DataBind()
End If
End Sub
Protected Sub dlCustomers_OnItemDataBound(ByVal sender As Object, ByVal e As DataListItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim item As DataListItem = TryCast(e.Item, DataListItem)
Dim lateDays As String = (TryCast(item.FindControl("lblLateDays"), Label)).Text.Trim()
If lateDays = "0" Then
Dim tbl As HtmlTableCell = TryCast(item.FindControl("tbl"), HtmlTableCell)
tbl.BgColor = "Red"
End If
End If
End Sub
Screenshot