How do I change a listitem font color?

Discussion in 'ASP .Net' started by Do, Dec 8, 2003.

  1. Do

    Do Guest

    Hi,

    In my listbox, I'd like to change the color of some listitems
    depending on user security.

    I tried wrapping the <font color=red></font> around
    my listitme's .text property, but the tags show up in the page load.

    What can I do here?

    Do
     
    Do, Dec 8, 2003
    #1
    1. Advertisements

  2. Usually, you'd use something like:

    ListItem MyListItem = new ListItem("text", "value");
    MyListItem.Attributes.CssStyle.Add("color", "red");
    MyListBox.Add(MyListItem);

    However, there's a known bug in the standard ListBox and DropDownList
    classes, where item attributes are ignored. To get around this, you need to
    create a new class derived from the standard ListBox or DropDownList class
    and override it's RenderContents method to correctly render the attributes:

    override protected void RenderContents(HtmlTextWriter writer)
    {
    for(int c=0;c<Items.Count;c++)
    {
    ListItem i = Items[c];
    writer.WriteBeginTag("option");
    if(i.Selected) writer.WriteAttribute("selected", "selected", false);
    writer.WriteAttribute("value", i.Value, true);
    IEnumerator d = Items[c].Attributes.Keys.GetEnumerator();

    while(d.MoveNext())writer.WriteAttribute(d.Current.ToString(),Items[c].Attri
    butes[d.Current.ToString()]);
    writer.Write('>');
    System.Web.HttpUtility.HtmlEncode(i.Text, writer);
    writer.WriteEndTag("option");
    writer.WriteLine();
    }
    }

    (The code above is from
    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=u4Yvw5wzBHA.2520@tkmsftngp05)

    Hope this helps,

    Mun




    "Do" <> wrote in message
    news:...
    > Hi,
    >
    > In my listbox, I'd like to change the color of some listitems
    > depending on user security.
    >
    > I tried wrapping the <font color=red></font> around
    > my listitme's .text property, but the tags show up in the page load.
    >
    > What can I do here?
    >
    > Do
     
    Munsifali Rashid, Dec 8, 2003
    #2
    1. Advertisements

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. Sreejumon[MVP]

    Change font color in Datagrid?

    Sreejumon[MVP], Jul 8, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    547
    Sreejumon[MVP]
    Jul 8, 2003
  2. makthar

    Change font color in Datagrid?

    makthar, Jul 9, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    494
    makthar
    Jul 9, 2003
  3. mg

    ListItem color change - RB

    mg, Nov 11, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    8,013
  4. Mad Scientist Jr
    Replies:
    2
    Views:
    676
    Mad Scientist Jr
    Jun 29, 2004
  5. Jim in Arizona
    Replies:
    7
    Views:
    1,167
    Badass Scotsman
    Nov 22, 2006
  6. mttc
    Replies:
    2
    Views:
    2,677
    Roedy Green
    Jul 3, 2009
  7. mg

    ListItem color change - RB

    mg, Nov 11, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    231
  8. Kamaljeet Saini
    Replies:
    0
    Views:
    846
    Kamaljeet Saini
    Feb 13, 2009
Loading...