Declare a local variable at the beginning of the View: @{ var yourStyle = “”; if(Model.Condition) { yourStyle = “float:left;”; //specific conditions and logic } else { yourStyle = “float:right;”; //any other conditions and logic } } and then just use it in your div: <div style=”@yourStyle”>
Tag: if
The Ternary Operator in C# (?:)
C# includes a decision-making operator ?: called the conditional operator or ternary operator. It is the short form of the if-else conditions. Syntax: condition ? statement 1 : statement 2 The ternary operator starts with a boolean condition. If this condition evaluates to true then it will execute the first statement after ?, otherwise the second statement after : will […]
How to use ? : if statements with Asp.Net Razor
<span class=”btn btn-@(item.Value == 1 ? “primary” : item.Value == 2 ? “warning” : “danger”)”>EVALUATE</span> Briefly, we can say that @(condition ? “the value, if the condition is true” : “the value, if the condition is false” )
How to use Switch in Reportviewer
= Switch(Fields!YourField.Value = “Value1”, “The result of the first condition”, Fields!YourField.Value = “Value2”, “The result of the second condition”, Fields!YourField.Value = “Value3”, “The result of the third condition”, 1 = 1 , “That is for the default value” ) 1=1 refers to default in switch case. IF you want to use IIF then You […]
How to Validate a DateTime in C#?
DateTime myDate; if(DateTime.TryParse(txtBirthDate.Text, out myDate)) { //Valid Date } else { //Invalid Date }
Determine If string contains more than 1 value in C#
Let’s say you have a condition like that: if ((myString.Contains(“Value1”) && (myString.Contains(“Value2”)) && (myString.Contains(“Value3”))) { … } Basically, you want to check if all of your values are contained in the string . Fortunately (with the help of LINQ), this can by translated almost literally into C#: var values = new String[] {“Value1”, “Value2”, “Value3”}; if […]
How to run code if not in Debug Mode in C#
Sometimes you want to run your code only when it is not in debug mode. You can use #IF DEBUG code block for this purpose #if DEBUG LogMail(“It is in debug mode, only add a log record.”) #else SendMail(“It is not in debug mode, so you can send e-mail.”) #endif