Thursday, March 22, 2012

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!On Mar 19, 4:06 pm, Rekha <R...@.discussions.microsoft.com> wrote:
> Hi,
> I am trying to change the font color for the items in a dropdownlist contr
ol
> 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;
On Mar 19, 9:06 pm, Rekha <R...@.discussions.microsoft.com> wrote:
> Hi,
> I am trying to change the font color for the items in a dropdownlist contr
ol
> 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.
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 p
rogrammatically.
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 espaol : http://asp.net.do/foros/
===================================
"Alexey Smirnov" <alexey.smirnov@.gmail.com> wrote in message
news:1174339905.643056.214630@.y66g2000hsf.googlegroups.com...
> On Mar 19, 9:06 pm, Rekha <R...@.discussions.microsoft.com> wrote:
> It's a known bug, it doesn't work.
>

0 comments:

Post a Comment