Pregunta

I have a problem with one of the code of my application. I have a Item Category Block in my application. Which goes like this

NewsITEM1 (2) | NewsITEM2 (0) | NewsITEM3 (14)

The above represents ITEM Description and (Count_of_The_Item) under bracket.

Now The user need some customization , they need (Count_of_The_Item) to be Red whenever the count is other that "0" . For example in NewsItem1(2) and NewsItem3(14) -- (2) and (14) should be in RED where as in NewsITEM2 (0) -- (0) should come as General Black.

 string newsCount = sectionInfo.NewsItemsCount.ToString();


  if (bookmarkForCategory.Length > 0) bookmarkForCategory.Append(" | ");

 bookmarkForCategory.Append("<a href='#" + title + "'>" + title + "</a>" + " (" + newsCount + ")");

In the above code you could see the newsCount variable is populating. With Normal CSS properties it is getting printed as Black. Now we want to make it RED whenever the newsCount variable value is other that "0" .

Requesting your help to resolve this.

¿Fue útil?

Solución

Something like this should work with the example code you've posted, if you set the styles in whatever css file you are loading.

int newsCount = sectionInfo.NewsItemsCount;  
if (bookmarkForCategory.Length > 0) bookmarkForCategory.Append(" | ");
var itemMarkup = "<a href='#{0}'>{0}</a><span{1}>({2})</span>";
bookmarkForCategory.AppendFormat(itemMarkup, 
      title, newsCount > 0 ? " style='color:red;'" : "", newsCount);

Couple notes though, there are more proper ways to create markup, and you definitely want to be wary of building html directly like this if any of those values are coming from untrusted sources (like user input).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top