Avatar of nmcdermaid
nmcdermaid asked on

Bind a label's backcolor to a database field

Hi,

Using ASP.Net and Visual Studio 2005, I am attempting to dynamically change the backgournd colour of a label.

In the smart tag for the Label, I have selected 'Edit Databindings'
Then I have selected the Backcolor property, and assigned to a column (WebColour) from my table.

It has added this code to my label tags:

Backcolor='<%# Eval("WebColour) %>'


I've tried three different data formats in my table:

#000000
000000
0

All of them result in a 'specified cast is not valid' error at the backcolor tag.


When I type

? Eval("WebColour)

in the command window I see the exact data that I am expecting.


Can anyone tell me what format this needs to be in in that database?

Also can you tell me what the # symbol is doing in the statement... I understand the rest.


The label is inside a FormView control which is working fine in every other way.


.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
nmcdermaid

8/22/2022 - Mon
ASKER
nmcdermaid

I just tried named colours and got the same error message
ASKER
nmcdermaid

hmmmmm don't tell me I have to cast it to the internal VB colour class.
SOLUTION
Pratima

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
digitalZo

Not sure if this is going to solve your problem, but Isn't there a closing quote missing?

It should be:
Backcolor='<%# Eval("WebColour") %>'
Your help has saved me hundreds of hours of internet surfing.
fblack61
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Alpesh Patel

Can u place your .aspx code over here so that can be more clarified to give you ansewer.
thanx
ASKER
nmcdermaid

The code is on a different machine, so I missed a closing quote when I typed it in.

All the code has been automatically generated, and I prefer to keep it that way if possible. I'm primarily interested in what the actual data needs to be in the database to make it work.

The entire code for the cell is currently:

<td style="width: 10%">
  <asp:Label ID="Label1" runat="server" BackColor='<%# Eval("WebColour") %>'
  Style="text-align: center; font-weight: bold;"
  Text='<%# Eval("Rating") %>' Width="98%"></asp:Label>
</td>




I followed pratimas suggestion and changed it to this:

<td style="width: 10%">
  <asp:Label ID="Label1" runat="server"  
  Style="text-align: center; font-weight: bold; backcolor: <%# Eval("WebColour") %>"
  Text='<%# Eval("Rating") %>' Width="98%"></asp:Label>
</td>

But I'm getting a 'tag is not well formed' error.


Pratima, what is the syntax to case it to that data type?


.. and can anyone tell me what the # symbol is in the Eval statement?
ASKER
nmcdermaid

I got it to work by putting the colour assignment in the TD tag... along the lines of pratimas suggestion

So anyway, it looks like to bind the backcolour I need to use the colour datatype. If I wanted to stilll use a constant in the db, what would the syntax be to convert a #RRGGBB colour to the required colour data type?

and can anyone tell me the significance of the # symbol in the Eval statement?

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
nmcdermaid

Fantastic, that makes sense now... the # is part of the opening tag rather than the eval function.

Thanks for that.

Regarding my other question (data format of a bound backcolour), it sounds like I would need to cast it to a .Net datatype anyway, which basically is too complex to bother with.

Its strange that the editor lets you bind it, but apparently its not valid... you need to add code to get it to work.


Thanks for your help.