簡體   English   中英

在 aspx 頁面中使用 if else 和 eval

[英]using if else with eval in aspx page

有沒有辦法在 aspx 頁面上的以下 eval 中使用 else if。

目前我的 div 如下:

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

我想在我的 div 上使用以下邏輯:

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

我試過這樣的事情,但它不起作用:

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

我想知道在aspx頁面上是否可以進行這樣的操作。 我不能在 aspx.cs 中做到這一點。

如果您絕對不想使用代碼隱藏,您可以為此嘗試條件運算符:

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

那是假設字段Percentage包含整數。

更新: VB.NET 版本,以防萬一,由tomasofen提供:

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

你可以試試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>

如果您嘗試綁定一個 Model 類,您可以向它添加一個新的 readonly 屬性,例如:

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

否則,您可以使用 Andrei's 或 kostas ch。 如果您不能修改類本身,則建議

<%# (string)Eval("gender") =="M" ? "Male" :"Female"%>
 <%if (System.Configuration.ConfigurationManager.AppSettings["OperationalMode"] != "live") {%>
                        &nbsp;[<%=System.Environment.MachineName%>]
                        <%}%>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM