Solved

Setting DropDownList.BackColor property

Posted on 2006-07-17
15
749 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 300 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 100 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 100 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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