Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using if else with eval in aspx page

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.

like image 286
CodeNinja Avatar asked Jun 12 '13 13:06

CodeNinja


People also ask

What is eval in ASPX?

Eval function is used to bind data to control inside a DataBound control, but it cannot update the values back to the database. Bind function can be used to bind data to control inside a DataBound control and also it can update the values back to the database.

What is eval in asp net GridView?

Eval) function is used to bind data in controls inside GridView, DataList, Repeater, DetailsView, etc. and using string. Format multiple values can be set to a single control. HTML Markup. The below HTML Markup consists of an ASP.Net GridView with 4 columns.

What is EVAL C#?

Eval(Object, String, String) Evaluates data-binding expressions at run time and formats the result as a string. public: static System::String ^ Eval(System::Object ^ container, System::String ^ expression, System::String ^ format); C# Copy.


2 Answers

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")) %> 
like image 119
Andrei Avatar answered Oct 04 '22 20:10

Andrei


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> 
like image 25
kostas ch. Avatar answered Oct 04 '22 19:10

kostas ch.