Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there a way to use an else if on the following eval on the aspx page .

Currently my div is as follows :

  <div class="tooltip" style="display: none">                                                                  
        <div style="text-align: center; font-weight: normal">
                Value = <%# Eval("Percentage") + "%" %>     
        </div>
  </div>

I would like to use the following logic on my div :

If(Percentage < 50)
   display "0 %"
   else 
   display "percentage"

I tried something like this but it doesn't work :

if (<%# Eval("Percentage") %> < 50)
{
    Eval("0");
}
else
{
   <%# Eval("PassPercentage") + "%" %> 
 }

I want to know if such an operation is possible to do on the aspx page. I cannot do it in aspx.cs.

share|improve this question
5  
Why is it not possible to use code-behind class for this? – Andrei 2 days ago
I am using the div to display in jquery tool tip . I would like to do it on the aspx page . If its possible. – SqlSamurai 2 days ago
You could use a ternary operator – emd 2 days ago

3 Answers

up vote 6 down vote accepted

If you absolutely do not want to use code-behind, you can try conditional operator for this:

<%# ((int)Eval("Percentage") < 50) ? "0 %" : Eval("Percentage") %>

That is assuming field Percentage contains integer.

share|improve this answer
perfect ! this works ! – SqlSamurai 2 days ago

You can try c#

public string ProcessMyDataItem(object myValue)
 {
  if (myValue == null)
   {
   return "0 %"";
  }
   else
  {
     if(Convert.ToInt32(myValue) < 50)
       return "0";
     else
      return myValue.ToString() + "%";
  }

 }

asp

 <div class="tooltip" style="display: none">                                                                  
      <div style="text-align: center; font-weight: normal">
   Value =<%# ProcessMyDataItem(Eval("Percentage")) %> </div>
 </div>
share|improve this answer
+1 Thanks ! This is a working solution too . but I am looking for something do do on the aspx page itself. – SqlSamurai 2 days ago
You r welcome. Then Andrei post is suitable for you :) – kostas ch. 2 days ago

If you are trying to bind is a Model class, you can add a new readonly property to it like:

public string FormattedPercentage
{
    get
    {
        If(this.Percentage < 50)
            return "0 %";
        else 
            return string.Format("{0} %", this.Percentage)        
     }
}

Otherwise you can use Andrei's or kostas ch. suggestions if you cannot modify the class itself

share|improve this answer
Thanks ! This is helpful. but I am not using Percentage as a property. But it would be something that would be useful in the future. – SqlSamurai 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.