How to change color in TemplateField using Eval

My Gridview contains a lot of TemplateField.
I want every <td> in my html source to equal the color stored in my database
I am trying to execute code. Located below, but does not work, it gives me the <span> inside the <td> with my color, but it doesnโ€™t appear in 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 works

 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 html and css source 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; } 
+8
html css gridview
source share
3 answers

You need to put the text inside your label (which displays the range)

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

You can also use a panel (which displays a div) rather than a label. Remember to put things inside the div.

+4
source share

If you want to check with a boolean if it is true, the effect of Green color else Red color will act. Then display the text with the appropriate color according to the Eval function. Here GetStatus is the method that you need to create in the code behind with its text binding to the user interface, otherwise you can communicate with the Eval or Bind function, as usual.

  ForeColor='<%# (bool)Eval("UserType")==true?System.Drawing.Color.Green:System.Drawing.Color.Red %>' Text='<%# GetStatus((bool)Eval("UserType")) %>'> 
+3
source share
 ForeColor='<%# Convert.ToString(Eval("ESM")) == "Elective" ? System.Drawing.Color.Green: Convert.ToString(Eval("ESM")) == "Emergency" ? System.Drawing.Color.Red: System.Drawing.Color.Purple%>' 

Try this code ..........

+2
source share

All Articles