Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change color in TemplateField using Eval

I have a Gridview contain many TemplateField.
I want make every <td> in my html source equal the color saved in my database
I try code Located below but not working it's give me a <span> tag inside <td> with my color but But do not appear on the browser

<asp:TemplateField HeaderText="BackGround Color">
     <ItemTemplate>
           <asp:Label  ID="lblBackColor" runat="server" 
                   BackColor='<%# ConvertFromHexToColor( Eval("BackColor").ToString()) %>'>
            </asp:Label>
           <itemstyle width="20%" horizontalalign="Center" />
     </ItemTemplate>    
</asp:TemplateField>

C# code working

public  Color ConvertFromHexToColor(string hex)
{
    string colorcode = hex;
    int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
    Color clr = Color.FromArgb(argb);
    return clr;
}

And this is the source html and css code in my browser

            <td>
                <span id="BodyZone__ThemesGrid_lblForeColor_0"  style="background-color: #FFFFFF;"></span>
                <itemstyle width="20%" horizontalalign="Center">
                    </itemstyle>
            </td>

CSS

table.activity_datatable td {
padding: 8px 15px;
color: #6c6c6c;
vertical-align: middle;
-webkit-transition: all 0.2s;
}
like image 714
Tarek Saied Avatar asked Feb 11 '13 07:02

Tarek Saied


1 Answers

If you want to check with a boolean value if it is true then Green colour else Red colour will effect. Then displaying text with the respective colour according to the Eval function. Here GetStatus is a method you need to create it in code behind with its we are binding the text to UI, or else you can bind with Eval or Bind function as usual.

 ForeColor='<%# (bool)Eval("UserType")==true?System.Drawing.Color.Green:System.Drawing.Color.Red %>'
                                Text='<%# GetStatus((bool)Eval("UserType")) %>'>
like image 102
Krish Avatar answered Nov 04 '22 01:11

Krish