31

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.

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54
CodeNinja
  • 3,188
  • 19
  • 69
  • 112

5 Answers5

64

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.

Update: Version for VB.NET, just in case, provided by tomasofen:

<%# If(Eval("Status") < 50, "0 %", Eval("Percentage")) %>
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • The user was asking for C#, but i arrive here from search engine looking for VB.NET version, so i suppose this can help "another me" in the future: <%# If(Eval("Status") < 50, "0 %", Eval("Percentage")) %> Enjoy it – tomasofen May 03 '20 at 00:52
19

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>
kostas ch.
  • 1,960
  • 1
  • 17
  • 30
  • +1 Thanks ! This is a working solution too . but I am looking for something do do on the aspx page itself. – CodeNinja Jun 12 '13 at 19:06
2

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

shake
  • 1,752
  • 4
  • 18
  • 22
  • 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. – CodeNinja Jun 12 '13 at 19:07
1
<%# (string)Eval("gender") =="M" ? "Male" :"Female"%>
Ehtasham
  • 54
  • 7
  • 4
    Generally avoid code-only answers. Consider adding a `description` or at least show more context that helps to explain your code. Thanks –  Apr 14 '15 at 03:17
0
 <%if (System.Configuration.ConfigurationManager.AppSettings["OperationalMode"] != "live") {%>
                        &nbsp;[<%=System.Environment.MachineName%>]
                        <%}%>
apomene
  • 14,282
  • 9
  • 46
  • 72