change conditional fore color in eval

Last Reply 4 days ago By dharmendr

Posted 5 days ago

hello i wnat to make oclor change conditionally, in eval inside listview

 

so if value of eval is a, it should red and if value is b so it should green 

 

please advice

                                           <asp:Label ID="lblplaytype" runat="server" Text='<%#Eval("type") %>' ForeColor='<%#Eval("type").ToString().Replace("a",System.Drawing.Color.Red) %>'></asp:Label>

 

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

Hi nauna,

Check this example. Now please take its reference and correct your code.

HTML

C#

<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1">
    <LayoutTemplate>
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th>Type</th>
            </tr>
            <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
        </table>
    </LayoutTemplate>
    <GroupTemplate>
        <tr><asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder></tr>
    </GroupTemplate>
    <ItemTemplate>
        <td>
            <asp:Label ID="lblplaytype" runat="server" Text='<%#Eval("type") %>'
                ForeColor='<%#Eval("type").ToString().ToLower()== "a"?System.Drawing.Color.Red:System.Drawing.Color.Green %>'></asp:Label>
        </td>
    </ItemTemplate>
</asp:ListView>

VB.Net

<asp:ListView ID="ListView1" runat="server" GroupPlaceholderID="groupPlaceHolder1" ItemPlaceholderID="itemPlaceHolder1">
    <LayoutTemplate>
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th>Type</th>
            </tr>
            <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
        </table>
    </LayoutTemplate>
    <GroupTemplate>
        <tr><asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder></tr>
    </GroupTemplate>
    <ItemTemplate>
        <td>
            <asp:Label ID="lblplaytype" runat="server" Text='<%#Eval("type") %>'
                ForeColor='<%# If(Eval("type").ToString().ToLower() = "a", System.Drawing.Color.Red, System.Drawing.Color.Green) %>'></asp:Label>
        </td>
    </ItemTemplate>
</asp:ListView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        dt.Columns.Add("type");
        dt.Rows.Add("a");
        dt.Rows.Add("b");
        dt.Rows.Add("b");
        dt.Rows.Add("a");
        ListView1.DataSource = dt;
        ListView1.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As System.Data.DataTable = New System.Data.DataTable()
        dt.Columns.Add("type")
        dt.Rows.Add("a")
        dt.Rows.Add("b")
        dt.Rows.Add("b")
        dt.Rows.Add("a")
        ListView1.DataSource = dt
        ListView1.DataBind()
    End If
End Sub

Output

Type
a
b
b
a