Solved

Setting DropDownList.BackColor property

Posted on 2006-07-17
15
730 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now