简体   繁体   中英

Inline If Then Statement. ASP.NET

I need to make a statement much like

<%#((bool)Eval("inactive")) ? "<span style='color:red;font-weight:600;'>INACTIVE</span>" : "<span style='color:green;font-weight:600;'>ACTIVE</span>"%>  

Except instead of a boolean I need it to take 3 Conditional Statements.

So using

<%#Eval("Program_Num") %>  

I need it to say

  • If Program_Num == 1 then it's X,
  • if Program_Num == 2 then it's y,
  • if Program_Num == 3 then it's z;

I'll clarify and answer any questions to the best of my abilities, thank you for the help.

Try something like this.

<%# (int)Eval("Program_Num") == 1 ? "X" : (int)Eval("Program_Num") == 2 ? "Y" : (int)Eval("Program_Num") == 3 ? "Z" : "default value" %>

It's like saying this.

if ((int)Eval("Program_Num") == 1)
    "X"
else if ((int)Eval("Program_Num") == 2)
    "Y"
else if ((int)Eval("Program_Num") == 3)
    "Z"
else
    "default value"

You can try the following:

<%#Eval("Program_Num") == 1 ? "X" : Eval("Program_Num") == 2 ? "y" : Eval("Program_Num") == 3 ? "z" %> 

Obviously you need to substitute "x" "y" "z" with whatever you want it display.

It's inelegant, but you could just use a nested version of it:

var x = Program_Num == 1 ? "X" : (Program_Num == 2 ? "x" : (Program_Num == 3 ? "y" : "DEFAULT"))

I've kept it as clean C# for clarity here. You can adapt it for ASP.NET.

If you're going to three levels, then it's really time to start using proper if or switch constructs though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM