Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having a Labels in my Datalist ItemTemplate.

How to Change the Color of the label based upon the text displayed..

i'm using Eval Property to assign text.. which was from db table..

if the text is break then color should be red else it should be green

How to Achieve it in .aspx page plz help..

i don't want to use row data bound for it
Posted
Updated 27-Jan-14 18:51pm
v4

well you can use datalist 's item data bound event to see the text of the lable and apply color based on your condition.

see more on item databound on google


https://www.google.co.in/#q=datalist%20itemdatabound
 
Share this answer
 
v2
Comments
Member 10517120 28-Jan-14 2:46am    
ForeColor='<%# Convert.ToString(Eval("Subject_Name")) == "Break" ? System.Drawing.Color.Magenta :Convert.ToString(Eval("Subject_Name")) == "Not Assigned" ? System.Drawing.Color.Red: System.Drawing.Color.Green %>' this worked for me
Try setting the BackColor or ForeColor property of Label like

BackColorr='<%# Convert.ToString(Eval("Fieldname")) == "Break" ? "red" :"green" %>' 
// change the fieldname ,color and also format accordingly
Or
BackColorr='<%# Convert.ToString(Eval("Fieldname")) == "Break" ? System.Drawing.ColorTranslator.FromHtml("#FDEEEE") :System.Drawing.ColorTranslator.FromHtml("#FFFFF") %>'

Or even you can use a c# method and just call it in the Markup .

Other alternative is to handle in the RowDataBound event and FindControl the Label control and set the color.
 
Share this answer
 
v4
Comments
Member 10517120 28-Jan-14 1:01am    
wow joCodes You are Master
JoCodes 28-Jan-14 1:04am    
Probably a learner ;)
JoCodes 28-Jan-14 1:05am    
Did it solve the issue?
Member 10517120 28-Jan-14 1:07am    
I need to Check

actually if the text is "Break" or "Not Assigned" color should be Red

Else Green. and the Column Name is 'Subject_Name'
JoCodes 28-Jan-14 1:09am    
Ok, try it out and let me know .

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