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
Avatar of nmcdermaid
nmcdermaid

ASKER

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

ASKER

hmmmmm don't tell me I have to cast it to the internal VB colour class.
SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of digitalZo
digitalZo
Flag of India image

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

It should be:
Backcolor='<%# Eval("WebColour") %>'
SOLUTION
Avatar of Pratima
Pratima
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Alpesh Patel
Alpesh Patel
Flag of United States of America image

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

ASKER

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?
Avatar of nmcdermaid
nmcdermaid

ASKER

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?

ASKER CERTIFIED SOLUTION
Avatar of Alpesh Patel
Alpesh Patel
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of nmcdermaid
nmcdermaid

ASKER

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.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo