.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Change the fore color of an item in a repeater based on the status.

Posted By:satyapriyanayak       Posted Date: June 04, 2013    Points: 25    Category:    URL: http://www.dotnetspark.com  

we will know how to Change the fore color of an item in a repeater based on the status.
 

we will know how to Change the fore color of an item in a repeater based on the status.


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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" >
    <HeaderTemplate>
   <table border="1" cellpadding="0" cellspacing="0">
    </HeaderTemplate>
     <ItemTemplate>
               
      <tr style="color: <%# GetRowColor(DataBinder.Eval(Container.DataItem, "status")) %>">
      <td width="70px" align="center" valign="middle">
       <asp:Label ID="lblID" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ID") %>' />
       </td>
      <td valign="middle" width="70px">
       <%#DataBinder.Eval(Container.DataItem, "status") %>
      </td>
       <td valign="middle" width="70px">
       <%#DataBinder.Eval(Container.DataItem, "name") %>
       </td>
      <td valign="middle" width="70px">
    <%#DataBinder.Eval(Container.DataItem, "address")%>
            </td>
         </tr>
         </ItemTemplate>
    <FooterTemplate>
       </table>
      </FooterTemplate>
            </asp:Repeater>
    </div>
    </form>
</body>
</html>


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = CreateDataSource();
            Repeater1.DataSource = dt;
            Repeater1.DataBind();
        }
    }
    protected DataTable CreateDataSource()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("ID", typeof(string)));
        dt.Columns.Add(new DataColumn("status", typeof(string)));
        dt.Columns.Add(new DataColumn("name", typeof(string)));
        dt.Columns.Add(new DataColumn("address", typeof(string)));
        dr = dt.NewRow();
        dr["ID"] = "E001";
        dr["status"] = "1";
        dr["name"] = "Raj";
        dr["address"] = "pune";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["ID"] = "E002";
        dr["status"] = "2";
        dr["name"] = "ravi";
        dr["address"] = "mumbai";
        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["ID"] = "E003";
        dr["status"] = "3";
        dr["name"] = "rahul";
        dr["address"] = "puri";
        dt.Rows.Add(dr);

        return dt;
    }
    public string GetRowColor(object obj)
    {
        string color = "white";
        if (!string.IsNullOrEmpty(obj.ToString()))
        {
            string status = obj.ToString();
            switch (status)
            {
                case "1":
                    color = "red";

                    break;
                case "2":
                    color = "green";

                    break;

                case "3":
                    color = "blue";

                    break;
                default:
                    color = "white";
                    break;
            }
        }
        return color;
    }
}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend