setting the font color to red for Dropdownlist control list item

Discussion in 'ASP .Net' started by =?Utf-8?B?UmVraGE=?=, Mar 19, 2007.

  1. 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.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!
    =?Utf-8?B?UmVraGE=?=, Mar 19, 2007
    #1
    1. Advertising

  2. =?Utf-8?B?UmVraGE=?=

    bpd Guest

    On Mar 19, 4:06 pm, Rekha <> wrote:
    > 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.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;
    bpd, Mar 19, 2007
    #2
    1. Advertising

  3. On Mar 19, 9:06 pm, Rekha <> wrote:
    > 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.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.
    Alexey Smirnov, Mar 19, 2007
    #3
  4. 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" <> wrote in message
    news:...
    > On Mar 19, 9:06 pm, Rekha <> wrote:
    >> 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.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.
    >
    Juan T. Llibre, Mar 19, 2007
    #4
  5. Re: setting the font color to red for Dropdownlist control list it

    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" <> wrote in message
    > news:...
    > > On Mar 19, 9:06 pm, Rekha <> wrote:
    > >> 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.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.
    > >

    >
    >
    >
    =?Utf-8?B?UmVraGE=?=, Mar 20, 2007
    #5
  6. Re: setting the font color to red for Dropdownlist control list it

    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" <> wrote in message
    news:...
    > 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" <> wrote in message
    >> news:...
    >> > On Mar 19, 9:06 pm, Rekha <> wrote:
    >> >> 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.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.
    >> >

    >>
    >>
    >>
    Juan T. Llibre, Mar 20, 2007
    #6
  7. Re: setting the font color to red for Dropdownlist control list it

    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" <> wrote in message
    > news:...
    > > 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" <> wrote in message
    > >> news:...
    > >> > On Mar 19, 9:06 pm, Rekha <> wrote:
    > >> >> 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.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.
    > >> >
    > >>
    > >>
    > >>

    >
    >
    >
    =?Utf-8?B?UmVraGE=?=, Mar 20, 2007
    #7
  8. =?Utf-8?B?UmVraGE=?=

    Eswar Guest

    Solution

    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
    Eswar, Mar 26, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mttc
    Replies:
    2
    Views:
    2,371
    Roedy Green
    Jul 3, 2009
  2. M. Craig

    How to set background color of List item in dropdown list

    M. Craig, Nov 12, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    280
    M. Craig
    Nov 12, 2003
  3. M. Craig
    Replies:
    1
    Views:
    731
    Victor Garcia Aprea [MVP]
    Jan 9, 2004
  4. fig000
    Replies:
    0
    Views:
    4,533
    fig000
    Sep 6, 2004
  5. Kamaljeet Saini
    Replies:
    0
    Views:
    391
    Kamaljeet Saini
    Feb 13, 2009
Loading...

Share This Page