By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,505 Members | 1,165 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,505 IT Pros & Developers. It's quick & easy.

How do I change a listitem font color?

P: n/a
Do
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
Nov 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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=e...%40tkmsftngp05)

Hope this helps,

Mun


"Do" <do*************@hotmail.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
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


Nov 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.