Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dynamically change the textcolor of each item in listbox in asp.net with c#
Posted
Updated 13-Mar-12 2:37am
v2
Comments
shreekar 13-Mar-12 10:09am    
Please show code you have tried so far and where it is failing.
Shahin Khorshidnia 13-Mar-12 10:27am    
dynamically change the textcolor of each item in listbox in asp.net with c#

1 solution

If you have static ListItems in aspx:
ASP.NET
<asp:listitem style="color:green;">2</asp:listitem>

Visual Studio will complain about 'style' is not allowed attribute but it will render fine.
If you have databound ListBox you can do this in 'ondatabound' event:

C#
protected void ListBox1_DataBound(object sender, EventArgs e)
{
	ListBox lb = sender as ListBox;
	foreach (ListItem item in lb.Items)
	{
		item.Attributes["style"] = "color:yellow;";//  or whatever color you want
	}
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900