473,320 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

setting the font color to red for Dropdownlist control list item

Hi,

I am trying to change the font color for the items in a dropdownlist control
at run time using ASP.NET 2.0.

DropDownList1.Items[i].Attributes.Add("style", "color:red");

But when I run the application, I don't see any color change.

I am using Visual Studio 2005, ASP.NEt 2.0.

Any help would be great!
Mar 19 '07 #1
7 17337
bpd
On Mar 19, 4:06 pm, Rekha <R...@discussions.microsoft.comwrote:
Hi,

I am trying to change the font color for the items in a dropdownlist control
at run time using ASP.NET 2.0.

DropDownList1.Items[i].Attributes.Add("style", "color:red");

But when I run the application, I don't see any color change.

I am using Visual Studio 2005, ASP.NEt 2.0.

Any help would be great!
If you want the color for the entire list to be changed, then (in C#):
DropDownList1.ForeColor = Color.Red;

Mar 19 '07 #2
On Mar 19, 9:06 pm, Rekha <R...@discussions.microsoft.comwrote:
Hi,

I am trying to change the font color for the items in a dropdownlist control
at run time using ASP.NET 2.0.

DropDownList1.Items[i].Attributes.Add("style", "color:red");

But when I run the application, I don't see any color change.

I am using Visual Studio 2005, ASP.NEt 2.0.

Any help would be great!
It's a known bug, it doesn't work.

Mar 19 '07 #3
Something as simple as :
<option style="color: red;">Something</option>
....works.

Also, you can use a Listbox instead of a DropdownList if you want to do it programmatically.
They have roughly the same functionality, anyway.

myconnection = New SqlConnection("Server=YourServer;Integrated Security=True;Database=Northwind")
myda = New SqlDataAdapter("Select * from Products ", myconnection)
ds = New DataSet()
myda.Fill(ds, "AllTables")
dim i as Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
list1.Items.Add(New ListItem(ds.Tables(0).Rows(i)("UnitPrice"),
ds.Tables(0).Rows(i)("ProductID")))
If ds.Tables(0).Rows(i)("UnitPrice") <= 40 Then
list1.Items(i).Attributes.Add("style", "color:red")
Else
list1.Items(i).Attributes.Add("style", "color:green")
End If
Next

See it running at : http://asp.net.do/test/dropdowncolor2.aspx

Use anything you'd like for your conditions...


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
On Mar 19, 9:06 pm, Rekha <R...@discussions.microsoft.comwrote:
>Hi,

I am trying to change the font color for the items in a dropdownlist control
at run time using ASP.NET 2.0.

DropDownList1.Items[i].Attributes.Add("style", "color:red");

But when I run the application, I don't see any color change.

I am using Visual Studio 2005, ASP.NEt 2.0.

Any help would be great!

It's a known bug, it doesn't work.

Mar 19 '07 #4
Thanks to everyone for your reply.

I know that this is a bug but wasn't sure if Microsoft fixed the bug or not.
************************************************** **

"Juan T. Llibre" wrote:
Something as simple as :
<option style="color: red;">Something</option>
....works.

Also, you can use a Listbox instead of a DropdownList if you want to do it programmatically.
They have roughly the same functionality, anyway.

myconnection = New SqlConnection("Server=YourServer;Integrated Security=True;Database=Northwind")
myda = New SqlDataAdapter("Select * from Products ", myconnection)
ds = New DataSet()
myda.Fill(ds, "AllTables")
dim i as Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
list1.Items.Add(New ListItem(ds.Tables(0).Rows(i)("UnitPrice"),
ds.Tables(0).Rows(i)("ProductID")))
If ds.Tables(0).Rows(i)("UnitPrice") <= 40 Then
list1.Items(i).Attributes.Add("style", "color:red")
Else
list1.Items(i).Attributes.Add("style", "color:green")
End If
Next

See it running at : http://asp.net.do/test/dropdowncolor2.aspx

Use anything you'd like for your conditions...


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
On Mar 19, 9:06 pm, Rekha <R...@discussions.microsoft.comwrote:
Hi,

I am trying to change the font color for the items in a dropdownlist control
at run time using ASP.NET 2.0.

DropDownList1.Items[i].Attributes.Add("style", "color:red");

But when I run the application, I don't see any color change.

I am using Visual Studio 2005, ASP.NEt 2.0.

Any help would be great!
It's a known bug, it doesn't work.


Mar 20 '07 #5
re:
I know that this is a bug but wasn't sure if Microsoft fixed the bug or not.
*WAS* a bug ! :-)

In the previous example, I used a <SELECT directly:

<select name="DDL1" id="DDL1" size="10">

....but ASP.NET renders a dropdownlist control as a select, so there's
no problem getting a dropdownlist to render its contents in different colors.

I modified the example so it uses an ASP.NET dropdownlist control:

http://asp.net.do/test/dropdowncolor3.aspx

DDL1.Items.Add(New ListItem(ds.Tables(0).Rows(i)("UnitPrice"), ds.Tables(0).Rows(i)("ProductID")))
If ds.Tables(0).Rows(i)("UnitPrice") <= 40 Then
DDL1.Items(i).Attributes.Add("style", "color:red")
Else
DDL1.Items(i).Attributes.Add("style", "color:green")
End If
Next

That, and changing the <SELECT to :
<asp:DropDownList id="DDL1" size="10" runat="server" />

....produces the exact same HTML as the previous example.

View the source for

http://asp.net.do/test/dropdowncolor2.aspx
and
http://asp.net.do/test/dropdowncolor3.aspx

to verify that.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Rekha" <Re***@discussions.microsoft.comwrote in message
news:EB**********************************@microsof t.com...
Thanks to everyone for your reply.

I know that this is a bug but wasn't sure if Microsoft fixed the bug or not.
************************************************** **

"Juan T. Llibre" wrote:
>Something as simple as :
<option style="color: red;">Something</option>
....works.

Also, you can use a Listbox instead of a DropdownList if you want to do it programmatically.
They have roughly the same functionality, anyway.

myconnection = New SqlConnection("Server=YourServer;Integrated Security=True;Database=Northwind")
myda = New SqlDataAdapter("Select * from Products ", myconnection)
ds = New DataSet()
myda.Fill(ds, "AllTables")
dim i as Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
list1.Items.Add(New ListItem(ds.Tables(0).Rows(i)("UnitPrice"),
ds.Tables(0).Rows(i)("ProductID")))
If ds.Tables(0).Rows(i)("UnitPrice") <= 40 Then
list1.Items(i).Attributes.Add("style", "color:red")
Else
list1.Items(i).Attributes.Add("style", "color:green")
End If
Next

See it running at : http://asp.net.do/test/dropdowncolor2.aspx

Use anything you'd like for your conditions...


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@y66g2000hsf.googleg roups.com...
On Mar 19, 9:06 pm, Rekha <R...@discussions.microsoft.comwrote:
Hi,

I am trying to change the font color for the items in a dropdownlist control
at run time using ASP.NET 2.0.

DropDownList1.Items[i].Attributes.Add("style", "color:red");

But when I run the application, I don't see any color change.

I am using Visual Studio 2005, ASP.NEt 2.0.

Any help would be great!

It's a known bug, it doesn't work.



Mar 20 '07 #6
Juan,

Thanks for your reply.

The issue that I have is I am updating the list box items color based on the
selection made in a calendar field. When the user selects a date in the
calendar field, the list box items are color coded. I am updating the listbox
items in the Calendar_SelectionChanged event but I don't see the color
changing.

Any ideas!!

Thanks!

"Juan T. Llibre" wrote:
re:
I know that this is a bug but wasn't sure if Microsoft fixed the bug or not.

*WAS* a bug ! :-)

In the previous example, I used a <SELECT directly:

<select name="DDL1" id="DDL1" size="10">

....but ASP.NET renders a dropdownlist control as a select, so there's
no problem getting a dropdownlist to render its contents in different colors.

I modified the example so it uses an ASP.NET dropdownlist control:

http://asp.net.do/test/dropdowncolor3.aspx

DDL1.Items.Add(New ListItem(ds.Tables(0).Rows(i)("UnitPrice"), ds.Tables(0).Rows(i)("ProductID")))
If ds.Tables(0).Rows(i)("UnitPrice") <= 40 Then
DDL1.Items(i).Attributes.Add("style", "color:red")
Else
DDL1.Items(i).Attributes.Add("style", "color:green")
End If
Next

That, and changing the <SELECT to :
<asp:DropDownList id="DDL1" size="10" runat="server" />

....produces the exact same HTML as the previous example.

View the source for

http://asp.net.do/test/dropdowncolor2.aspx
and
http://asp.net.do/test/dropdowncolor3.aspx

to verify that.


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Rekha" <Re***@discussions.microsoft.comwrote in message
news:EB**********************************@microsof t.com...
Thanks to everyone for your reply.

I know that this is a bug but wasn't sure if Microsoft fixed the bug or not.
************************************************** **

"Juan T. Llibre" wrote:
Something as simple as :
<option style="color: red;">Something</option>
....works.

Also, you can use a Listbox instead of a DropdownList if you want to do it programmatically.
They have roughly the same functionality, anyway.

myconnection = New SqlConnection("Server=YourServer;Integrated Security=True;Database=Northwind")
myda = New SqlDataAdapter("Select * from Products ", myconnection)
ds = New DataSet()
myda.Fill(ds, "AllTables")
dim i as Integer
For i = 0 To ds.Tables(0).Rows.Count - 1
list1.Items.Add(New ListItem(ds.Tables(0).Rows(i)("UnitPrice"),
ds.Tables(0).Rows(i)("ProductID")))
If ds.Tables(0).Rows(i)("UnitPrice") <= 40 Then
list1.Items(i).Attributes.Add("style", "color:red")
Else
list1.Items(i).Attributes.Add("style", "color:green")
End If
Next

See it running at : http://asp.net.do/test/dropdowncolor2.aspx

Use anything you'd like for your conditions...


Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
"Alexey Smirnov" <al************@gmail.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...
On Mar 19, 9:06 pm, Rekha <R...@discussions.microsoft.comwrote:
Hi,

I am trying to change the font color for the items in a dropdownlist control
at run time using ASP.NET 2.0.

DropDownList1.Items[i].Attributes.Add("style", "color:red");

But when I run the application, I don't see any color change.

I am using Visual Studio 2005, ASP.NEt 2.0.

Any help would be great!

It's a known bug, it doesn't work.



Mar 20 '07 #7
I have tried Attributes.Add as well as CSSStyle.Add both worked for me

<asp:Dropdownlist id="ddlLimit" tabIndex="6" runat="server">
<asp:ListItem Value="100" Selected="True">100</asp:ListItem>
<asp:ListItem Value="250" style="color: #00AAAA;">250</asp:ListItem>
<asp:ListItem Value="500">500</asp:ListItem>
</asp:dropdownlist>

On page load

ddlLimit.Items[0].Attributes.Add("style", "color: #0000FF; font-size: 12pt");
ddlLimit.Items[1].Attributes.Add("style", "color: #00FFFF; font-size: 12pt");
ddlLimit.Items[2].Attributes.CssStyle.Add("color", "red");

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Mar 26 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dixie | last post by:
I want to be able to set the font size and font type for text in a text box on a report using VBA. I wan't to be able to control it from a setting in a table. The problem is that I don't know the...
2
by: Chris | last post by:
I was wondering if there was any way to add a blank list item control to the beginning of the System.Web.UI.WebControls.DropDownList's datasource after the control's datasource has been specified....
2
by: Ali | last post by:
I am adding a DropDownList control to my DataGrid footer template. I load my DropDownList using a function that returns an arrayList. I can see all my items in the DropDownList, but when I select...
6
by: Oscar | last post by:
I want to add items to a dropdownlist control within a Javascript eventhandler. This is what I code : var dd = document.getElementById("DropDownList1"); dd.Items.Add("1990");...
1
by: Matt | last post by:
Hi. I'm using VS2005 and working with the Dropdownlist control on an ASP page. My dropdownlist has several thousand items, all integers, ranging from 1000 to 80000 (these represent different...
2
by: glenn | last post by:
Hi folks, I am trying to determine which item in a DropDownList Web control has been selected. I have posted an OnSelectedIndexChanged subroutine in my code with a reference to the subroutine...
1
by: Mike | last post by:
Hi I am using predefined membership and roles managemant approach characteristic for framework 2.0 Basic idea is that we select some value from DropDownList which represents a role and we get...
2
Ali Rizwan
by: Ali Rizwan | last post by:
Hi all, I have a label and i have a font not installed and i want to set that font to my label. How can i do this? I don't wanna install the font. Using vb6.0 XP Thanx
1
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have a page that displays a DropDownList control that allows a Manager Name to be selected from a listing of Managers for a User. So, on my main user.aspx page, I include a DropDownList control...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.