How do I change a listitem font color?

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

  1. Do

    Do Guest


    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, Dec 8, 2003
    1. Advertisements

  2. Usually, you'd use something like:

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

    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];
    if(i.Selected) writer.WriteAttribute("selected", "selected", false);
    writer.WriteAttribute("value", i.Value, true);
    IEnumerator d = Items[c].Attributes.Keys.GetEnumerator();

    System.Web.HttpUtility.HtmlEncode(i.Text, writer);

    (The code above is from[email protected])

    Hope this helps,

    Munsifali Rashid, Dec 8, 2003
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.