ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Change ASP.Net DataList Cell Background Color based on condition using C# and VB.Net<p>Hi <a class="username" rel="akhter"> akhter</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:DataList ID=&#34;dlCustomers&#34; runat=&#34;server&#34; RepeatLayout=&#34;Table&#34; OnItemDataBound=&#34;dlCustomers_OnItemDataBound&#34;&gt; &lt;HeaderTemplate&gt; &lt;table class=&#34;table&#34;&gt; &lt;tr&gt; &lt;td&gt; TDID &lt;/td&gt; &lt;td&gt; TDDesc &lt;/td&gt; &lt;td&gt; taskdate &lt;/td&gt; &lt;td&gt; enddate &lt;/td&gt; &lt;td&gt; TDstatus &lt;/td&gt; &lt;td&gt; latedays &lt;/td&gt; &lt;/tr&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;tr&gt; &lt;td&gt; &lt;%# Eval(&#34;TDID&#34;)%&gt; &lt;/td&gt; &lt;td&gt; &lt;%# Eval(&#34;TDDesc&#34;)%&gt; &lt;/td&gt; &lt;td&gt; &lt;%# Eval(&#34;taskdate&#34;)%&gt; &lt;/td&gt; &lt;td&gt; &lt;%# Eval(&#34;enddate&#34;)%&gt; &lt;/td&gt; &lt;td&gt; &lt;%# Eval(&#34;TDstatus&#34;)%&gt; &lt;/td&gt; &lt;td runat=&#34;server&#34; id=&#34;tbl&#34;&gt; &lt;asp:Label ID=&#34;lblLateDays&#34; Text=&#39;&lt;%# Eval(&#34;latedays&#34;)%&gt;&#39; runat=&#34;server&#34; /&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;/table&gt; &lt;/FooterTemplate&gt; &lt;/asp:DataList&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data; using System.Web.UI.HtmlControls;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data </pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { DataTable dt = new DataTable(); dt.Columns.Add(&#34;TDID&#34;); dt.Columns.Add(&#34;TDDesc&#34;); dt.Columns.Add(&#34;taskdate&#34;); dt.Columns.Add(&#34;enddate&#34;); dt.Columns.Add(&#34;TDstatus&#34;); dt.Columns.Add(&#34;latedays&#34;); dt.Rows.Add(1, &#34;hey&#34;, &#34;01/02/2019&#34;, &#34;04/05/2019&#34;, &#34;Pending&#34;, &#34;1&#34;); dt.Rows.Add(2, &#34;Hi&#34;, &#34;05/03/2019&#34;, &#34;08/06/2019&#34;, &#34;USA&#34;, &#34;0&#34;); 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(&#34;lblLateDays&#34;) as Label).Text.Trim(); if (lateDays == &#34;0&#34;) { HtmlTableCell tbl = item.FindControl(&#34;tbl&#34;) as HtmlTableCell; tbl.BgColor = &#34;Red&#34;; } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">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(&#34;TDID&#34;) dt.Columns.Add(&#34;TDDesc&#34;) dt.Columns.Add(&#34;taskdate&#34;) dt.Columns.Add(&#34;enddate&#34;) dt.Columns.Add(&#34;TDstatus&#34;) dt.Columns.Add(&#34;latedays&#34;) dt.Rows.Add(1, &#34;hey&#34;, &#34;01/02/2019&#34;, &#34;04/05/2019&#34;, &#34;Pending&#34;, &#34;1&#34;) dt.Rows.Add(2, &#34;Hi&#34;, &#34;05/03/2019&#34;, &#34;08/06/2019&#34;, &#34;USA&#34;, &#34;0&#34;) 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(&#34;lblLateDays&#34;), Label)).Text.Trim() If lateDays = &#34;0&#34; Then Dim tbl As HtmlTableCell = TryCast(item.FindControl(&#34;tbl&#34;), HtmlTableCell) tbl.BgColor = &#34;Red&#34; End If End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://imgur.com/rnb0e3b.jpg" alt="" width="393" height="293" /></p>https://www.aspforums.net:443/Threads/207969/Change-ASPNet-DataList-Cell-Background-Color-based-on-condition-using-C-and-VBNet/https://www.aspforums.net:443/Threads/207969/Change-ASPNet-DataList-Cell-Background-Color-based-on-condition-using-C-and-VBNet/Thu, 16 May 2019 06:01:35 GMT