Utilizing the eval Function in ASPX Pages with If-Else Statements

Feedback


Solution 1:

If you are not inclined to utilize code-behind, an alternative approach would be to employ the conditional operator.

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

Provided that

Percentage

is comprised of integers.

In case needed, tomasofen has provided a version for VB.NET as an update.

<%# If(Eval("Status") < 50, "0 %", Eval("Percentage")) %>


Solution 2:

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

 


Solution 3:


To bind a Model class, it is possible to create a new property that is read-only.

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

If making modifications to the class is not possible, you have the option to implement the suggestions provided by Andrei or kostas ch.

Frequently Asked Questions

Posted in Uncategorized