Change back color of repeater control row based on the date

Last Reply 4 days ago By pandeyism

Posted 4 days ago

Hi, I have a repeater control and I would like to change the back color of the entire row based on the date(Next Due Date).

<asp:Panel ID="pnlAllMedicals" CssClass="panel panel-info"  runat="server" Visible="false">

                    <div class="panel-heading" style="text-align:center;">
                        <h4 class="panel-title">
          <a data-toggle="collapse" href="#collapse1">All Medicals</a>
        </h4>
                    </div>
                <div class="table-responsive">
            <asp:Repeater ID="rpAllMedicals" runat="server" OnItemDataBound="rpAllMedicals_ItemDataBound">
            <HeaderTemplate>
                <table class="table table-striped table-bordered table-hover table-condensed" border="1">
                <thead>
                    <tr>
                        <th>Department</th>
                        <th>Cost Centre</th>
                        <th>Name</th>
                        <th>Surname</th>
                        <th>Clock No</th>
                        <th>Next Due Date</th>
                        <th>Date Tested</th>
                        <th>ECG</th>
                        <th>Lungfunction</th>
                        <th>Hearing Test</th>
                        <th>Eye Test</th>
                    </tr>
                </thead>
                <tbody>
            </HeaderTemplate>
            <ItemTemplate>
                <tr>
                    <td><%#DataBinder.Eval(Container.DataItem, "Department")%></td>
                    <td><%#DataBinder.Eval(Container.DataItem, "Cost Centre")%></td>
                    <td><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
                    <td><%#DataBinder.Eval(Container.DataItem, "Surname")%></td>
                    <td><%#DataBinder.Eval(Container.DataItem, "Clock No")%></td>                  
                    <td><%#FormatDate(DataBinder.Eval(Container.DataItem, "Next Due Date"))%></td>
                    <td><%#FormatDate(DataBinder.Eval(Container.DataItem, "Date Tested"))%></td>
                    <td><%#FormatDate(DataBinder.Eval(Container.DataItem, "ECG"))%></td>
                    <td><%#FormatDate(DataBinder.Eval(Container.DataItem, "Lungfunction"))%></td>
                    <td><%#FormatDate(DataBinder.Eval(Container.DataItem, "Hearing Test"))%></td>
                    <td><%#FormatDate(DataBinder.Eval(Container.DataItem, "Eye Test"))%></td>

                </tr>                                           
            </ItemTemplate>                                         
            <FooterTemplate>
                </tbody>
                </table>
            </FooterTemplate>
            </asp:Repeater>
            </div>


                </asp:Panel>

 

You are viewing reply posted by: pandeyism 4 days ago.
Posted 4 days ago

Hi Tevin,

Refer below sample.

HTML

<asp:Repeater ID="rptCustomers" runat="server" OnItemDataBound="rptCustomers_ItemDataBound">
    <HeaderTemplate>
        <table class="table table-striped table-bordered table-hover table-condensed" border="1">
            <thead>
                <tr>
                    <th>
                        Name
                    </th>
                    <th>
                        Next Due Date
                    </th>
                </tr>
            </thead>
            <tbody>
    </HeaderTemplate>
    <ItemTemplate>
        <tr id="row">
            <td>
                <%#DataBinder.Eval(Container.DataItem, "Name")%>
            </td>
            <td>
                <asp:Label Text='<%#Eval("Next Due Date")%>' runat="server" ID="lblDate" />
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </tbody> </table>
    </FooterTemplate>
</asp:Repeater>

Code

C#

using System.Data;
using System.Drawing;

VB.Net

Imports System.Data
Imports System.Drawing

Namespaces

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("Name", typeof(string)), new DataColumn("Next Due Date", typeof(DateTime)) });
        dt.Rows.Add("Mudassar Khan", DateTime.Today);
        dt.Rows.Add("John Hammod", DateTime.Today.AddDays(1));
        dt.Rows.Add("Suzanne Mathews", DateTime.Today.AddDays(2));
        dt.Rows.Add("Robert Schidner", DateTime.Today.AddDays(3));
        rptCustomers.DataSource = dt;
        rptCustomers.DataBind();
    }
}

protected void rptCustomers_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        RepeaterItem item = e.Item;
        Label lbldate = item.FindControl("lblDate") as Label;
        if (Convert.ToDateTime(lbldate.Text) == DateTime.Today)
        {
            lbldate.BackColor = Color.Red;
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Me.IsPostBack Then
            Dim dt As DataTable = New DataTable()
            dt.Columns.AddRange(New DataColumn() {New DataColumn("Name", GetType(String)), New DataColumn("Next Due Date", GetType(DateTime))})
            dt.Rows.Add("Mudassar Khan", DateTime.Today)
            dt.Rows.Add("John Hammod", DateTime.Today.AddDays(1))
            dt.Rows.Add("Suzanne Mathews", DateTime.Today.AddDays(2))
            dt.Rows.Add("Robert Schidner", DateTime.Today.AddDays(3))
            rptCustomers.DataSource = dt
            rptCustomers.DataBind()
        End If
    End Sub

    Protected Sub rptCustomers_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            Dim item As RepeaterItem = e.Item
            Dim lbldate As Label = TryCast(item.FindControl("lblDate"), Label)

            If Convert.ToDateTime(lbldate.Text) = DateTime.Today Then
                lbldate.BackColor = Color.Red
            End If
        End If
End Sub

Screenshot