 |
| ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 9th, 2006, 11:41 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
DropDownlist and Hilighting
Is it possible to give differing colors to the items in a DropDownList object (say yellow)?. I want certain items in my list to be yellow (background/hilighting?) based on database information while others are standard colors, etc.
Any help would be greatly appreciated.
Thanks,
David
__________________
Thanks,
David
|
|

February 9th, 2006, 04:51 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
You can use style tags to give the items a different color. E.g.:
<option style="background-color: red;">Some Option</option>
<option style="background-color: green;">Some Option</option>
To change these style settings, you need to hook some event in your Code Behind. What event this is, depends on the location where the DropDownList control is used.
If it's placed directly in the page, you can loop through the control's Items collection.
If the item is inside, say, a GridView, you'll need to use the grid's ItemDataBound event.
HtH,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Have a little faith in me(bonus live) by Ilse De Lange (Track 14 from the album: Here I Am - 1998-2003) What's This?
|
|

February 9th, 2006, 05:22 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
OK. This seems to work, except that once a postback occurs, the style is lost
I see alot of postings doing a search that suggests this is a bug in VS.
IS there a way to get around this?
Thanks,
David
|
|

February 9th, 2006, 05:29 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Can you show some code, so we can see how you add the styles?
It's probably not a bug; the styles are not persisted in view state but the data is. So, on post back, the data is added to the drop down again, but ItemDataBound is not fired (because you're not databinding again).
I think there are two ways to fix this: rebind on every page load (thus including post backs) or loop through the drop-down diagnosing the Items on each post back.
Cheers,
Imar
P.S. If it was a bug, it would be a .NET Framework bug; VS.NET has nothing to do with this.
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Sick Sad Little World by Incubus (Track 6 from the album: A Crow Left Of The Murder...) What's This?
|
|

February 10th, 2006, 11:17 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
bit of code inside the !Page.ISPostBack of the PAGE_LOAD
dr = sc.ExecuteReader();
while (dr.Read())
{
ListItem li = new ListItem(dr["FULLNAME"].ToString() + " | " + dr["INC_NUMBER_ID"].ToString() + " | " + dr["INC_DATE_OCCURED"].ToString(), dr["INC_NUMBER_ID"].ToString());
if ((Convert.ToBoolean(dr["INC_CAUSE_DEFECTIVE_EQUIP"]) == false) &&
(Convert.ToBoolean(dr["INC_CAUSE_INADEQUATE_PROC"]) == false) &&
(Convert.ToBoolean(dr["INC_CAUSE_PERSONAL_RESP"]) == false))
{
Color color = Color.Yellow;
string htmlColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
li.Attributes.Add("style", String.Format("background-color: {0}", htmlColor));
}
else
{
Color color = Color.White;
string htmlColor = string.Format("#{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B);
li.Attributes.Add("style", String.Format("background-color: {0}", htmlColor));
}
inclist.Items.Add(li);
}
dr.Close();
Thanks,
David
|
|

February 10th, 2006, 12:16 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Right, that makes sense.
So, I guess my earlier assumption is true. Since styles are not persisted in ViewState, they're not applied if you don't rebind the data on PostBack.
The problem in your scenario is that the values of your drop-downs are not (exclusively) used for determining the right styles. So, on PostBack, you cannot simply through the Items collection of the drop-down, diagnose the Value or Text properties and reapply the styles.
You could fix that by rebinding the data on PostBack as well, but then you loose changes the user made in the selected indices.
What you could is, on PostBack, get the data from the DataSource again, and the use that data to reapply the styles to the drop-down list items, without rebinding the data to the control. Not a pretty solution, but it might do the trick.
Cheers,
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

February 10th, 2006, 01:46 PM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
That seemed to do it.
:D
Thanks,
David
|
|

February 10th, 2006, 01:53 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
For the purpose of the archive, can you "what" did it?
Did you get the data on PostBack again?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
|
|

February 11th, 2006, 11:53 AM
|
|
Authorized User
|
|
Join Date: Feb 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I Followed the advice on Postback to :
get the data from the DataSource again, and the use that data to
reapply the styles to the drop-down list items
"That" does it.
Thanks,
David
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| dropdownlist |
sumith |
ASP.NET 1.0 and 1.1 Professional |
1 |
February 23rd, 2007 09:11 AM |
| DropdownList |
Venkatesan |
ASP.NET 1.0 and 1.1 Basics |
1 |
December 11th, 2006 11:03 AM |
| how to get value from dropdownlist |
vandat |
ASP.NET 1.0 and 1.1 Basics |
3 |
November 28th, 2005 12:47 AM |
| DropDownList |
chiraagb |
VS.NET 2002/2003 |
5 |
June 15th, 2004 03:12 AM |
|
 |