Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET razor VB logic


May 12, 2021 ASP.NET


Table of contents


ASP.NET Razor - VB logical condition

ASP.NET Razor VB logical conditions can execute code according to the appropriate conditions.


Programming logic: Code is executed according to conditions.


If condition

VB allows code to be executed according to conditions.

Use the if statement to determine the condition. Based on the judgment, the if statement returns true or false:

  • The if statement starts with a block of code
  • The condition is written between if and then
  • If the condition is true, if ... The code between then and end if is executed
@Code
Dim price=50
End Code
<html>
<body>
@If price>30 Then
@<p>The price is too high.</p>
End If
</body>
</html>

Run an instance . . .


Else condition

The if statement can contain the else condition.

The else condition defines the code that is executed when the condition is false.

@Code
Dim price=20
End Code
<html>
<body>
@if price>30 then
@<p>The price is too high.</p>
Else
@<p>The price is OK.</p>
End If
</body>
</htmlV>

Run an instance . . .

Note: In the example above, if the first condition is true, the code for the if block will be executed. The else condition overrides "all cases" except the if condition.


ElseIf condition

Several conditions determine that the elseif condition can be used:

@Code
Dim price=25
End Code
<html>
<body>
@If price>=30 Then
@<p>The price is high.</p>
ElseIf price>20 And price<30
@<p>The price is OK.</p>
Else
@<p>The price is low.</p>
End If
</body>
</html>

Run an instance . . .

In the example above, if the first condition is true, the code for the if block will be executed.

If the first condition is not true and the second condition is true, the code for the elseif block will be executed.

There is no limit to the number of elseif conditions.

If neither the if and elseif conditions are true, the last else block (without conditions) overrides "all other cases."


Select condition

Select blocks can be used to test some individual conditions:

@Code
Dim weekday=DateTime.Now.DayOfWeek
Dim day=weekday.ToString()
Dim message=""
End Code
<html>
<body>
@Select Case day
Case "Monday"
message="This is the first weekday."
Case "Thursday"
message="Only one day before weekend."
Case "Friday"
message="Tomorrow is weekend!"
Case Else
message="Today is " & day
End Select
<p> @message </p>
</body>
</html>

Run an instance . . .

Select Case is followed by the test value (day). E ach individual test condition has a case value and any number of lines of code. I f the test value matches the case value, the corresponding line of code is executed.

The select block has a default case (Case Else), which overrides "all other situations" when all specified cases do not match.

The above is ASP.NET the use of Razor VB logical conditions.