Mido
Mido

Reputation: 131

Bind a Label's BackColor a Hex color value from the database

i'm looking to get a BackColor of Label in ASP.NET Form and Change this label's Color with this value is that possible ?

<asp:Label ID="Label2" runat="server" Text="Label" BackColor='<%# System.Drawing.Color.FromName(Eval(Container.DataItem,"BgColor").ToString())%>'></asp:Label>

thanks.

Upvotes: 1

Views: 11774

Answers (5)

amir ameri
amir ameri

Reputation: 21

you should used ColorHex like : #602A4D And not used .Tostring() to this code :

BackColor='<%# System.Drawing.Color.FromName(Eval("BgColor"))%>'>

Upvotes: 0

Vishnu
Vishnu

Reputation: 71

//Label, setting forecolor by HexaColor

lblResult.ForeColor = System.Drawing.ColorTranslator.FromHtml("#E93301");

or

//Label, setting forecolor by color Name

lblResult.ForeColor = System.Drawing.Color.Red;

Upvotes: 7

Zach Green
Zach Green

Reputation: 3460

You should be using FromHtml:

<%# System.Drawing.ColorTranslator.FromHtml(Eval("BgColor")%>

Upvotes: 4

Mido Ahmed
Mido Ahmed

Reputation: 26

the format is 3300FF in database

'<%# System.Drawing.Color.FromName(Eval("BgColor").ToString())%>'

but tha label display none color

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94645

You may try,

<asp:Label 
     ID="Label2" 
     runat="server" 
     Text="Label" 
     BackColor='<%# System.Drawing.Color.FromName(Eval("BgColor").ToString())%>'>
</asp:Label>

Upvotes: 1

Related Questions