Skip to main content

How to change the row color of the Repeater based on some condition?

<%@ Page Language="C#" %>

<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            Repeater1.DataSource = GetCustomMadeDataTable();
            Repeater1.DataBind();
        }
    }
    protected string FormatColorRow(string strYear)
    {


        if (int.Parse(strYear) <= 2005)
        {
            return "style='backGround-color:red'";
        }
        else
        {
            return "style='backGround-color:green'";
        }
    }

    public DataTable GetCustomMadeDataTable()
    {

        //Create a new DataTable object

        System.Data.DataTable objDataTable = new System.Data.DataTable();

        //Create three columns with string as their type

        objDataTable.Columns.Add("ISBN", typeof(string));
        objDataTable.Columns.Add("Title", typeof(string));
        objDataTable.Columns.Add("Publisher", typeof(string));
        objDataTable.Columns.Add("Year", typeof(string));

        DataColumn[] dcPk = new DataColumn[1];
        dcPk[0] = objDataTable.Columns["ISBN"];
        objDataTable.PrimaryKey = dcPk;
        objDataTable.Columns["ISBN"].AutoIncrement = true;
        objDataTable.Columns["ISBN"].AutoIncrementSeed = 1;
        //Adding some data in the rows of this DataTable
        DataRow dr;
        for (int i = 1; i <= 10; i++)
        {
            dr = objDataTable.NewRow();

            dr[1] = "Title" + i.ToString();
            dr[2] = "Publisher" + i.ToString();
            dr[3] = "200" + i.ToString();
            objDataTable.Rows.Add(dr);
        }

        Session["strTemp"] = objDataTable;
        return objDataTable;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server" EnableViewState="False">
                <HeaderTemplate>
                    <table>
                </HeaderTemplate>
                <ItemTemplate>
               
                    <tr &lt;%#FormatColorRow(DataBinder.Eval(Container.DataItem,"Year").ToString())%&gt;>
                        <td> <%# DataBinder.Eval(Container.DataItem,"Title").ToString() %></td>
                        <td> <%# DataBinder.Eval(Container.DataItem,"Publisher").ToString() %></td>
                        <td>
                            <%# DataBinder.Eval(Container.DataItem,"Year").ToString() %>
                        </td>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>
    </form>
</body>
</html>

Comments

Popular posts from this blog