Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Setting DropDownList.BackColor property

Posted on 2006-07-17
15
Medium Priority
?
760 Views
Last Modified: 2008-03-04
Hi
I am trying to set the back color of a dropdownlist but giving me an incorrect format error

the format is the usual (#FF0000) Etc
           
 DropDownList.DataValueField = "HtmlColor";
 DropDownList.DataTextField = "HtmlColor";          
 DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml("HtmlColor");

Any Idea?
0
Comment
Question by:JoeUS
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17122439
Hi JoeUS,

i think you need to supply a hex value in the FromHtml() method:

#ffffff - white
#000000 - black

Apresto
0
 
LVL 7

Expert Comment

by:skvikram
ID: 17122457
modify the code :
DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml("HtmlColor");

DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml(<% # HtmlColor %>);

0
 

Author Comment

by:JoeUS
ID: 17122465
You mean I can not assign back color values individually to the list elements?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 23

Expert Comment

by:apresto
ID: 17122477
i dont think so

that doesnt make any sense.

you need to pass in a colour representation

you just need to remove the quotes:

DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml("HtmlColor");

becomes

DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml( HtmlColor );
0
 
LVL 23

Expert Comment

by:apresto
ID: 17122482
do you mean you want to assign different options different colours?
0
 
LVL 7

Accepted Solution

by:
brdrok earned 1200 total points
ID: 17122905
check out the following link:

http://www.codeproject.com/aspnet/ColorListBox.asp

it might be able to point you in the right direction.

0
 

Author Comment

by:JoeUS
ID: 17122908
this code is in C# and on the code behind page. I am loading the values from a

DataSet that is why I need to use "HtmlColor" for the DropDownList component

These mods didn't solve the problem

DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml(<% # HtmlColor %>);
or
DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml( HtmlColor );

0
 
LVL 23

Expert Comment

by:apresto
ID: 17123011
what is the content of HtmlColor

if its a hex value it should be fine
0
 
LVL 23

Expert Comment

by:apresto
ID: 17123019
are you getting errors when you try the later suggestion?
0
 

Author Comment

by:JoeUS
ID: 17123205
The HtmlColor value is formated like this #FFFFFF and it of course changes with each listItem
The later suggestion gives me an out of context error. It seems like the DropDownList component can't process DataSet fields for background color. It does do fine when I give it a hard coded value (HEX #FF0000) but it doesn't take the same value from the DataSet
0
 
LVL 23

Expert Comment

by:apresto
ID: 17123279
use HtmlColor.ToString() and see if that works
0
 

Author Comment

by:JoeUS
ID: 17125311
HtmlColor is a DB coulumn name and the format is correct for these two properties
DropDownList.DataValueField = "HtmlColor";
DropDownList.DataTextField = "HtmlColor";
but
DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml(HtmlColor.ToString());
fails just like
DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml("HtmlColor");
0
 
LVL 23

Assisted Solution

by:apresto
apresto earned 400 total points
ID: 17128404
Ok...

in the FromHtml() you need to have a colour in this format #xxxxxx.  

By using "HtmlColor" you are just passing in text, so this is wrong
By passing in HtmlColor (without quotes) this represents a variable - what is in this variable?

try this and see if it works:

DropDownList.BackColor = System.Drawing.ColorTranslator.FromHtml("#ff0000");

and just see if it takes effect
0
 
LVL 7

Assisted Solution

by:skvikram
skvikram earned 400 total points
ID: 17128905
Htmlcolor is a variable which can be assigned to
DropDownList.DataValueField = "HtmlColor";
DropDownList.DataTextField = "HtmlColor";

but it should be converted to suitable hexcolor value type before it can be passed to System.Drawing.ColorTranslator.FromHtml() LIKE in this code

System.Drawing.Color c = System.Drawing.ColorTranslator.FromHtml( Htmlcolor );
DropDownList.BackColor = c;

Any Hex color value cannot be passed to BackColor property it has to be ARGB Value. like
Color [A=255, R=245, G=247, B=248]
0
 

Author Comment

by:JoeUS
ID: 17130062
I have found the solution and it was "brdrok" who pointed me to the right direction
Here is the link to achieve what I was looking for and some explanation why
DropdownList color properties don't do so well in a DataBind situation. I also apreciate
"apresto" and  "skvikram" comments and efforts to trubleshoot this MS Bug.

http://www.c-sharpcorner.com/Code/2003/July/DropDownListBox.asp
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question