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.

4
  • 6
    Why is it not possible to use code-behind class for this? Commented Jun 12, 2013 at 13:48
  • I am using the div to display in jquery tool tip . I would like to do it on the aspx page . If its possible. Commented Jun 12, 2013 at 13:51
  • You could use a ternary operator Commented Jun 12, 2013 at 13:52
  • Possible duplicate of ASP.NET using Bind/Eval in .aspx in If statement Commented Mar 28, 2017 at 22:32

5 Answers 5

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")) %>
Sign up to request clarification or add additional context in comments.

1 Comment

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
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>

1 Comment

+1 Thanks ! This is a working solution too . but I am looking for something do do on the aspx page itself.
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

1 Comment

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.
1
<%# (string)Eval("gender") =="M" ? "Male" :"Female"%>

1 Comment

Generally avoid code-only answers. Consider adding a description or at least show more context that helps to explain your code. Thanks
0
 <%if (System.Configuration.ConfigurationManager.AppSettings["OperationalMode"] != "live") {%>
                        &nbsp;[<%=System.Environment.MachineName%>]
                        <%}%>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.