Solved

Setting DropDownList.BackColor property

Posted on 2006-07-17
15
750 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
Technology Partners: 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 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

707 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